Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Old/LLTimer/lltimer.cpp
blob: ff20d793efe9afccc33480cf3079755f3cdcf90e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "Python.h"
#include "Numeric/arrayobject.h"

#include <pthread.h>
#include <signal.h>

static int lltimerState=0;
static int callbackSet=0;
static unsigned long lltimerMSecs=1000;
static PyObject * pyCbFunction;
static PyObject * arglist = Py_BuildValue("()");
static PyObject * LltimerError;
static pthread_t thread;

 

extern "C" 
{

  //adapted from csound5 code (Vercoe et.al)
  static void millisleep(size_t milliseconds)  {
    struct timespec ts;
    register size_t n, s;
    
    s = milliseconds / (size_t) 1000;
    n = milliseconds - (s * (size_t) 1000);
    n = (size_t) ((int) n * 1000000);
    ts.tv_sec = (time_t) s;
    ts.tv_nsec = (long) n;
    while (nanosleep(&ts, &ts) != 0)
      ;
  }

  //return a timestamp in msecs
  //this is a millisecond timestamp. 
  static timeval startTime;
  static int startTimeInit=0;
  static unsigned long timestamp()  {
    if (startTimeInit==0) {
      gettimeofday(&startTime,NULL);
      startTimeInit=1;
      return 0;
    } 
    timeval currTime;
    gettimeofday(&currTime,NULL);
    return (unsigned long)((currTime.tv_sec - startTime.tv_sec)*1000 + (currTime.tv_usec - startTime.tv_usec)/1000) ;
  }

  //this is a millisecond delta timestamp
  static timeval lastTime;
  static int startDTimeInit=0;
  static unsigned long dtimestamp()  {
    if (startDTimeInit==0) {
      gettimeofday(&lastTime,NULL);
      startDTimeInit=1;
      return 0;
    } 
    timeval currTime;
    gettimeofday(&currTime,NULL);
    unsigned long retval= (unsigned long)((currTime.tv_sec - lastTime.tv_sec)*1000 + (currTime.tv_usec - lastTime.tv_usec)/1000) ;
    lastTime.tv_sec=currTime.tv_sec;
    lastTime.tv_usec=currTime.tv_usec;
    return retval;
  }




  void * periodicTimer(void * ignore) {
    if (callbackSet==0) {
      printf("No callback set!\n");
    } else {
      //try to update the priority of scheduler
      struct sched_param param;
      param.sched_priority = 50;   // Is this the best number?
      int sresult = sched_setscheduler( 0, SCHED_RR, &param);
      printf("Reult for setscheduler %i\n",sresult);
 
     // Check we have done what we hoped.
      int sched = sched_getscheduler(0);
      switch (sched) {
      case SCHED_RR:
	printf( " RR scheduler loaded \n" );   // running as root I now get this.
	break;
      case SCHED_FIFO:
	printf( " FIFO scheudler loaded\n" );
	break;
      default:
	printf( " priority =  %d \n",sched );
      }
      
      
      //now loop on our callback
      unsigned long starttime=timestamp();
      unsigned long nexttime=starttime+lltimerMSecs;
      while (lltimerState==1) {
	PyEval_CallObject(pyCbFunction, arglist);
	unsigned long sleeptime = nexttime-timestamp();
	if (sleeptime>0) {
	  millisleep(sleeptime);
	} else {
	  printf("LLTIMER: Cannot keep up. Slowing timer from %li to %li\n",lltimerMSecs,lltimerMSecs*2);
	  lltimerMSecs*=2;
	}
	nexttime+=lltimerMSecs;
      }
    }
    return NULL;
  }    





  //creates and starts timer. 
  static PyObject *  lltimer_timeout_add(PyObject * self, PyObject * args) {
    int msecs=1000;
 
   //parse args to be sure it was called as lltimer.start(msecs)
    if (!PyArg_ParseTuple(args, "iO:timeout_add", &msecs,&pyCbFunction)) {
      printf("Error in LLTimer.start. Syntax: lltimer.timeout_add(msecs,callback)\n");
      return NULL;
    }

    lltimerMSecs=(unsigned long) msecs; 

    if (!PyCallable_Check(pyCbFunction)) {
      PyErr_SetString(PyExc_TypeError, "Callback function must be callable");
      return NULL;
    }
    Py_XINCREF(pyCbFunction);         /* Add a reference to new callback */
    callbackSet=1;

    if (lltimerState==0) {
      //create and start the timer (Thanks to Gary Scavone @ McGill Univ. and his RtAudio for help on this!)
      pthread_attr_t attr;
      pthread_attr_init(&attr);
      //pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
      pthread_attr_setschedpolicy(&attr, SCHED_RR);
      int err = pthread_create(&thread, &attr, periodicTimer, NULL);
      //pthread_attr_destroy(&attr);
      if (err) {
	printf("Error initializing pthread\n");
	lltimerState=0;
      } else {
	printf("PThread initialized\n");
	lltimerState=1;
      }
    }	else {
      printf("Timer already started\n");
    }
    
    return Py_BuildValue("i", lltimerState);
  }



  static PyObject *  lltimer_stop(PyObject * self, PyObject * args) {
    char *argStr;
    if (!PyArg_ParseTuple(args, "", &argStr)) {
      printf("Error in LLTimer stop. Syntax: stop()\n");
      return NULL;
    }
    lltimerState=0;
    return Py_BuildValue("i", 0);
  }


}  



static PyMethodDef LltimerMethods[] = {
  {"stop",  lltimer_stop, METH_VARARGS},
  {"timeout_add",  lltimer_timeout_add, METH_VARARGS},
  {NULL,      NULL}        /* Sentinel */
};


extern "C" 
{
  void  initlltimer() {
    PyObject *m, *d;
    m = Py_InitModule("lltimer", LltimerMethods);
    d = PyModule_GetDict(m);
    LltimerError = PyErr_NewException("lltimer.error", NULL, NULL);
    PyDict_SetItemString(d, "error", LltimerError);
    import_array();
  }
}