Commit 8437bd59 authored by thomas's avatar thomas
Browse files

no message


git-svn-id: https://svn.grrrr.org/ext/trunk@188 4d9ac71a-51e6-0310-8455-cad1006bcd31
parent 39a1a877
No preview for this file type
...@@ -353,6 +353,13 @@ public: ...@@ -353,6 +353,13 @@ public:
bool ShouldExit() const { return shouldexit; } bool ShouldExit() const { return shouldexit; }
void Yield() // yield to other threads (for cooperative multitasking)
#ifndef MAXMSP
{}
#else
;
#endif
class ThrMutex class ThrMutex
{ {
public: public:
...@@ -524,6 +531,9 @@ private: ...@@ -524,6 +531,9 @@ private:
qmsg *qhead,*qtail; qmsg *qhead,*qtail;
t_clock *qclk; t_clock *qclk;
ThrMutex qmutex; ThrMutex qmutex;
#ifdef MAXMSP
t_clock *yclk;
#endif
static void QTick(flext_base *th); static void QTick(flext_base *th);
void Queue(qmsg *m); void Queue(qmsg *m);
......
...@@ -143,6 +143,9 @@ flext_base::flext_base(): ...@@ -143,6 +143,9 @@ flext_base::flext_base():
qhead = qtail = NULL; qhead = qtail = NULL;
qclk = (t_clock *)(clock_new(this,(t_method)QTick)); qclk = (t_clock *)(clock_new(this,(t_method)QTick));
thrhead = thrtail = NULL; thrhead = thrtail = NULL;
#ifdef MAXMSP
yclk = (t_clock *)(clock_new(this,(t_method)QTick));
#endif
#endif #endif
} }
...@@ -172,6 +175,9 @@ flext_base::~flext_base() ...@@ -172,6 +175,9 @@ flext_base::~flext_base()
// send remaining pending messages // send remaining pending messages
while(qhead) QTick(this); while(qhead) QTick(this);
clock_free((object *)qclk); clock_free((object *)qclk);
#ifdef MAXMSP
clock_free((object *)yclk);
#endif
#endif #endif
if(inlist) delete inlist; if(inlist) delete inlist;
......
...@@ -12,6 +12,11 @@ WARRANTIES, see the file, "license.txt," in this distribution. ...@@ -12,6 +12,11 @@ WARRANTIES, see the file, "license.txt," in this distribution.
#include <flext.h> #include <flext.h>
#ifdef MAXMSP
#define SCHEDTICK 1
#include <sched.h>
#endif
bool flext_base::StartThread(void *(*meth)(thr_params *p),thr_params *p,char *methname) bool flext_base::StartThread(void *(*meth)(thr_params *p),thr_params *p,char *methname)
{ {
#ifdef _DEBUG #ifdef _DEBUG
...@@ -27,8 +32,12 @@ bool flext_base::StartThread(void *(*meth)(thr_params *p),thr_params *p,char *me ...@@ -27,8 +32,12 @@ bool flext_base::StartThread(void *(*meth)(thr_params *p),thr_params *p,char *me
delete p; delete p;
return false; return false;
} }
else else {
#ifdef MAXMSP
sched_yield();
#endif
return true; return true;
}
} }
void flext_base::PushThread() void flext_base::PushThread()
...@@ -39,6 +48,10 @@ void flext_base::PushThread() ...@@ -39,6 +48,10 @@ void flext_base::PushThread()
else thrhead = nt; else thrhead = nt;
thrtail = nt; thrtail = nt;
tlmutex.Unlock(); tlmutex.Unlock();
#ifdef MAXMSP
if(thrhead) clock_delay(yclk,SCHEDTICK);
#endif
} }
void flext_base::PopThread() void flext_base::PopThread()
...@@ -60,8 +73,15 @@ void flext_base::PopThread() ...@@ -60,8 +73,15 @@ void flext_base::PopThread()
} }
tlmutex.Unlock(); tlmutex.Unlock();
#ifdef MAXMSP
if(!thrhead) clock_unset(&yclk);
#endif
} }
#ifdef MAXMSP
void flext_base::Yield() { sched_yield(); }
#endif
class flext_base::qmsg class flext_base::qmsg
{ {
...@@ -123,6 +143,8 @@ void flext_base::QTick(flext_base *th) ...@@ -123,6 +143,8 @@ void flext_base::QTick(flext_base *th)
} }
#endif #endif
th->Yield();
th->qmutex.Lock(); th->qmutex.Lock();
while(th->qhead) { while(th->qhead) {
qmsg *m = th->qhead; qmsg *m = th->qhead;
...@@ -145,6 +167,10 @@ void flext_base::QTick(flext_base *th) ...@@ -145,6 +167,10 @@ void flext_base::QTick(flext_base *th)
delete m; delete m;
} }
th->qmutex.Unlock(); th->qmutex.Unlock();
#ifdef MAXMSP
if(th->thrhead) clock_delay(th->yclk,SCHEDTICK);
#endif
} }
void flext_base::Queue(qmsg *m) void flext_base::Queue(qmsg *m)
......
...@@ -14,6 +14,11 @@ WARRANTIES, see the file, "license.txt," in this distribution. ...@@ -14,6 +14,11 @@ WARRANTIES, see the file, "license.txt," in this distribution.
#include <windows.h> #include <windows.h>
#endif #endif
#ifdef MAXMSP
#include <Timer.h>
#include <Threads.h>
#endif
t_atom *flext_base::CopyList(int argc,const t_atom *argv) t_atom *flext_base::CopyList(int argc,const t_atom *argv)
{ {
int i; int i;
...@@ -26,7 +31,14 @@ void flext_base::Sleep(float s) ...@@ -26,7 +31,14 @@ void flext_base::Sleep(float s)
{ {
#ifdef NT #ifdef NT
::Sleep((long)(s*1000)); ::Sleep((long)(s*1000));
#else #elif defined MAXMSP
Delay(s*1000,NULL); UnsignedWide tick;
Microseconds(&tick);
double target = tick.hi*((double)(1<<16)*(double)(1<<16))+tick.lo+s*1.e6;
for(;;) {
Microseconds(&tick);
if(target <= tick.hi*((double)(1<<16)*(double)(1<<16))+tick.lo) break;
YieldToAnyThread();
}
#endif #endif
} }
\ No newline at end of file
...@@ -41,6 +41,8 @@ private: ...@@ -41,6 +41,8 @@ private:
t_symbol *bliptxt; t_symbol *bliptxt;
ThrCond cond; ThrCond cond;
int flag;
}; };
FLEXT_NEW_2("thread2",thread2,int,t_symptr) FLEXT_NEW_2("thread2",thread2,int,t_symptr)
...@@ -52,6 +54,8 @@ thread2::thread2(int del,t_symptr txt): ...@@ -52,6 +54,8 @@ thread2::thread2(int del,t_symptr txt):
stopit(false), stopit(false),
running(false),blipping(false) running(false),blipping(false)
{ {
flag = 0;
AddInAnything(); AddInAnything();
AddOutInt(); AddOutInt();
SetupInOut(); // set up inlets and outlets SetupInOut(); // set up inlets and outlets
...@@ -62,6 +66,8 @@ thread2::thread2(int del,t_symptr txt): ...@@ -62,6 +66,8 @@ thread2::thread2(int del,t_symptr txt):
void thread2::m_start(int st) void thread2::m_start(int st)
{ {
++flag;
post("start 1"); post("start 1");
if(running) { count = st; return; } if(running) { count = st; return; }
...@@ -90,6 +96,7 @@ void thread2::m_start(int st) ...@@ -90,6 +96,7 @@ void thread2::m_start(int st)
void thread2::m_stop() void thread2::m_stop()
{ {
post("stop 1"); post("stop 1");
post("flag = %i",flag);
cond.Lock(); cond.Lock();
stopit = true; stopit = true;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment