*/}}
Ver Fonte

pthread spin adaptation to windows critical section

ChengduLittleA há 1 ano atrás
pai
commit
a2e6c6655f
7 ficheiros alterados com 77 adições e 42 exclusões
  1. 20 20
      la_audio.c
  2. 6 6
      la_data.c
  3. 5 5
      la_interface.h
  4. 5 5
      la_kernel.c
  5. 29 2
      la_util.c
  6. 10 2
      la_util.h
  7. 2 2
      resources/la_widgets.c

+ 20 - 20
la_audio.c

@@ -201,7 +201,7 @@ void laui_NoiseNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn
 void IDN_ScopeInit(laSynthNodeScope* n, int NoCreate){
     if(!n->Display1){ n->Display1=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
     if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
-    pthread_spin_init(&n->Lock,0);
+    laSpinInit(&n->Lock);
     if(NoCreate){ return; }
     n->In1=laCreateInSocket("CH1",0); n->In2=laCreateInSocket("CH2",0);
     strSafeSet(&n->Base.Name,"SCOPE"); n->Time=5; n->Brightness1=0.7f; n->Brightness2=0.4f;
@@ -210,7 +210,7 @@ void IDN_ScopeInit(laSynthNodeScope* n, int NoCreate){
 void IDN_ScopeDestroy(laSynthNodeScope* n){
     laDestroyInSocket(n->In1); laDestroyOutSocket(n->In2); strSafeDestroy(&n->Base.Name);
     memFree(n->Display1); memFree(n->Display2);
-    pthread_spin_destroy(&n->Lock);
+    laSpinDestroy(&n->Lock);
 }
 int IDN_ScopeVisit(laSynthNodeScope* n, laNodeVisitInfo* vi){
     LA_GUARD_THIS_NODE(n,vi);
@@ -224,7 +224,7 @@ int IDN_ScopeEval(laSynthNodeScope* n){
     if(!n->Display2){ n->Display2=memAcquireSimple(sizeof(real)*LA_SYNTH_PLEN*2); }
     real* ch1,*ch2; INPUTPACKET(ch1,n->In1); INPUTPACKET(ch2,n->In2);
     int Frame=1<<(n->Time-1); TNS_CLAMP(Frame,1,LA_SYNTH_PLEN);
-    int Times=LA_SYNTH_PLEN/Frame; int sp=0; pthread_spin_lock(&n->Lock);
+    int Times=LA_SYNTH_PLEN/Frame; int sp=0; laSpinLock(&n->Lock);
     for(int t=0;t<Times;t++){
         real smin1=FLT_MAX,smax1=-FLT_MAX,smin2=FLT_MAX,smax2=-FLT_MAX;
         for(int i=0;i<Frame;i++){
@@ -239,7 +239,7 @@ int IDN_ScopeEval(laSynthNodeScope* n){
         n->NextSample++; if(n->NextSample>=LA_SYNTH_PLEN) n->NextSample=0;
     }
     n->FromSynth=MAIN.Audio->AudioEvalSynth;
-    pthread_spin_unlock(&n->Lock);
+    laSpinUnlock(&n->Lock);
     return 1;
 }
 void IDN_ScopeCopy(laSynthNodeScope* new, laSynthNodeScope* old, int DoRematch){
@@ -308,20 +308,20 @@ void laui_OutputNode(laUiList *uil, laPropPack *This, laPropPack *Extra, laColum
 
 void laRebuildSynthGraphs(){
     if(MAIN.Audio->CurrentSynth){
-        pthread_spin_lock(&MAIN.Audio->CurrentSynth->Lock);
+        laSpinLock(&MAIN.Audio->CurrentSynth->Lock);
         laRebuildPageEval(MAIN.Audio->CurrentSynth->Page);
-        pthread_spin_unlock(&MAIN.Audio->CurrentSynth->Lock);
+        laSpinUnlock(&MAIN.Audio->CurrentSynth->Lock);
     }
 }
 int laEvalSynthGraphs(){
     laSynth* ss; int any=0;
     memset(MAIN.Audio->AudioSamples,0,sizeof(real)*LA_SYNTH_PLEN);
     for(ss=MAIN.Audio->Synths.pFirst;ss;ss=ss->Item.pNext){
-        pthread_spin_lock(&ss->Lock);
-        if(!ss->Playing){ pthread_spin_unlock(&ss->Lock); continue; }
+        laSpinLock(&ss->Lock);
+        if(!ss->Playing){ laSpinUnlock(&ss->Lock); continue; }
         MAIN.Audio->AudioEvalSynth=ss; MAIN.Audio->AudioEvalTime=0; INITPACKET(ss->AudioSamples);
         laRunPage(ss->Page,1);
-        pthread_spin_unlock(&ss->Lock);
+        laSpinUnlock(&ss->Lock);
         for(int i=0;i<LA_SYNTH_PLEN;i++){
             MAIN.Audio->AudioSamples[i]+=ss->AudioSamples[i];
         }
@@ -343,9 +343,9 @@ void laaudio_DataCallback(ma_device* pDevice, void* pOutput, const void* pInput,
         out[i]=MAIN.Audio->AudioSamples[MAIN.Audio->NextAudioSample]/10;
         MAIN.Audio->NextAudioSample++;
     }
-    pthread_spin_lock(&MAIN.Audio->AudioStatusLock);
+    laSpinLock(&MAIN.Audio->AudioStatusLock);
     MAIN.Audio->AudioAny=any;
-    pthread_spin_unlock(&MAIN.Audio->AudioStatusLock);
+    laSpinUnlock(&MAIN.Audio->AudioStatusLock);
 }
 
 void laInitMiniAudio(){
@@ -381,11 +381,11 @@ void laInitMiniAudio(){
     MAIN.Audio->NextAudioSample=LA_SYNTH_PLEN;
     MAIN.Audio->AudioFrameInterval=1.0f/SampleRate;
     INITPACKET(MAIN.Audio->AudioSamples);
-    pthread_spin_init(&MAIN.Audio->AudioStatusLock,0);
+    laSpinInit(&MAIN.Audio->AudioStatusLock);
 
 }
 void laDeinitAudio(){
-    pthread_spin_destroy(&MAIN.Audio->AudioStatusLock);
+    laSpinDestroy(&MAIN.Audio->AudioStatusLock);
     ma_device_stop(&MAIN.Audio->AudioDevice);
     ma_device_uninit(&MAIN.Audio->AudioDevice);
 }
@@ -399,7 +399,7 @@ laSynth* laNewSynth(char* Name){
     strSafeSet(&ss->Name,Name);
     lstAppendItem(&MAIN.Audio->Synths,ss);
     memAssignRef(MAIN.Audio,&MAIN.Audio->CurrentSynth,ss);
-    pthread_spin_init(&ss->Lock,0);
+    laSpinInit(&ss->Lock);
     return ss;
 }
 void laRemoveSynth(laSynth* ss){ if(!ss) return;
@@ -410,7 +410,7 @@ void laRemoveSynth(laSynth* ss){ if(!ss) return;
     }laNotifyInstanceUsers(ss);
     while(ss->Page->Racks.pFirst){ laDestroyRack(ss->Page->Racks.pFirst); } memLeave(ss->Page); ss->Page=0;
     lstRemoveItem(&MAIN.Audio->Synths,ss);
-    pthread_spin_destroy(&ss->Lock);
+    laSpinDestroy(&ss->Lock);
     memLeave(ss);
 }
 
@@ -445,7 +445,7 @@ int OPINV_laSynthPlay(laOperator* a, laEvent* e){
     int play=0; if(str && strSame(str,"PLAY")) play=1;
     if(play){ MAIN.Audio->AudioAny=1; }
     
-    pthread_spin_lock(&ss->Lock); ss->Playing=play; pthread_spin_unlock(&ss->Lock);
+    laSpinLock(&ss->Lock); ss->Playing=play; laSpinUnlock(&ss->Lock);
     
     ma_device_start(&MAIN.Audio->AudioDevice); laNotifyInstanceUsers(ss);
     return LA_FINISHED;
@@ -502,11 +502,11 @@ void* laget_FirstSynth(void* unused1,void* unused2){
     return MAIN.Audio->Synths.pFirst;
 }
 void lapost_Synth(laSynth* ss){
-    pthread_spin_init(&ss->Lock,0);
+    laSpinInit(&ss->Lock);
 }
 int laget_SynthPlaying(laSynth* ss){
     int play=0;
-    pthread_spin_lock(&ss->Lock); play=ss->Playing; pthread_spin_unlock(&ss->Lock);
+    laSpinLock(&ss->Lock); play=ss->Playing; laSpinUnlock(&ss->Lock);
     return play;
 }
 void laset_CurrentSynth(laAudio* a,laSynth* s){
@@ -517,9 +517,9 @@ void la_AudioPreFrame(){
     if(MAIN.GraphNeedsRebuild){ laRebuildSynthGraphs(); }
     
     int anyaudio=0;
-    pthread_spin_lock(&MAIN.Audio->AudioStatusLock);
+    laSpinLock(&MAIN.Audio->AudioStatusLock);
     anyaudio=MAIN.Audio->AudioAny;
-    pthread_spin_unlock(&MAIN.Audio->AudioStatusLock);
+    laSpinUnlock(&MAIN.Audio->AudioStatusLock);
     if(!anyaudio){ ma_device_stop(&MAIN.Audio->AudioDevice); }
 }
 

+ 6 - 6
la_data.c

@@ -479,10 +479,10 @@ void laNotifyUsersPP(laPropPack *pp){
         if (iul->Remove == la_PropPanelUserRemover){
             laPanel *p = iul->Pointer.p;
             if (iul->Which == pp->LastPs->p && iul->FrameDistinguish == p->FrameDistinguish){
-                //pthread_spin_lock(&MAIN.csNotifier);
+                //laSpinLock(&MAIN.csNotifier);
                 if ((iul->Which->PropertyType & LA_PROP_SUB) || iul->ForceRecalc) laRecalcPanel(p);
                 else laRedrawPanel(p);
-                //pthread_spin_unlock(&MAIN.csNotifier);
+                //laSpinUnlock(&MAIN.csNotifier);
             }
         }
     }
@@ -505,9 +505,9 @@ void laNotifySubPropUsers(laProp *prop, void *Instance){
         if (iul->Remove == la_PropPanelUserRemover){
             laPanel *p = iul->Pointer.p;
             if (iul->Which == prop && iul->FrameDistinguish == p->FrameDistinguish){
-                //pthread_spin_lock(&MAIN.csNotifier);
+                //laSpinLock(&MAIN.csNotifier);
                 laRecalcPanel(p);
-                //pthread_spin_unlock(&MAIN.csNotifier);
+                //laSpinUnlock(&MAIN.csNotifier);
             }
         }
     }
@@ -538,9 +538,9 @@ void laThreadNotifyUsers(char *Path){
     laThreadNotifier *tn = CreateNew(laThreadNotifier);
     strCopyFull(tn->Path, Path);
 
-    //pthread_spin_lock(&MAIN.csNotifier);
+    //laSpinLock(&MAIN.csNotifier);
     lstAppendItem(&MAIN.ThreadNotifiers, tn);
-    //pthread_spin_unlock(&MAIN.csNotifier);
+    //laSpinUnlock(&MAIN.csNotifier);
 }
 
 void la_SetPropMathcerContext(laPanel *p){

+ 5 - 5
la_interface.h

@@ -489,7 +489,7 @@ STRUCTURE(LA){
 
     laSafeString *WorkingDirectory;
 
-    //pthread_spinlock_t csNotifier;
+    //SYSLOCK csNotifier;
     laListHandle ThreadNotifiers;
 
     //preference:
@@ -550,7 +550,7 @@ STRUCTURE(LA){
     //LLVMContextRef llvmContext;
     //LLVMModuleRef llvmModule;
 
-    //pthread_spinlock_t csMediaPlay;
+    //SYSLOCK csMediaPlay;
 
     int DetachedViewSwitch;
     int example_int;
@@ -1664,7 +1664,7 @@ STRUCTURE(laSynth){
     laRackPage* Page;
     real Length;
 
-    pthread_spinlock_t Lock;
+    SYSLOCK Lock;
     int Playing;
     real AudioEvalTime;
     real* AudioSamples;
@@ -1711,7 +1711,7 @@ STRUCTURE(laSynthNodeScope){
     laNodeInSocket* In2;
     real *Display1,*Display2; // each PLEN*2, upper/lower bounds.
     int NextSample;
-    pthread_spinlock_t Lock;
+    SYSLOCK Lock;
     laSynth* FromSynth;
 };
 
@@ -1728,7 +1728,7 @@ STRUCTURE(laAudio){
     laListHandle Synths;
     laSynth* CurrentSynth;
 
-    pthread_spinlock_t AudioStatusLock;
+    SYSLOCK AudioStatusLock;
     ma_device AudioDevice;
     laSynth* AudioEvalSynth;
     real AudioFrameInterval;

+ 5 - 5
la_kernel.c

@@ -835,7 +835,7 @@ int laGetReadyWith(laInitArguments* ia){
 
     logPrint("Trying to load WinTab for stylus input...\n"); int WtAvailable=1;
     if (!LoadWintab()){ logPrint("WinTab not available.\n"); WtAvailable=0; }
-	if (!gpWTInfoA(0, 0, NULL)){ logPrint("WinTab service is not available. (gpWTInfoA() returns 0).\n"); WtAvailable=0; }
+	elif (!gpWTInfoA(0, 0, NULL)){ logPrint("WinTab service is not available. (gpWTInfoA() returns 0).\n"); WtAvailable=0; }
     if (WtAvailable){ MAIN.WinTabAvailable=1; logPrint("WinTab service is available.\n"); }
 
 #endif
@@ -6972,15 +6972,15 @@ int la_HandleEvents(laWindow *w){
 
     MAIN.CurrentWindow=w;
 
-    //pthread_spin_lock(&MAIN.csNotifier);
+    //laSpinLock(&MAIN.csNotifier);
     while (tn = lstPopItem(&MAIN.ThreadNotifiers)){
-        //pthread_spin_unlock(&MAIN.csNotifier);
+        //laSpinUnlock(&MAIN.csNotifier);
         laNotifyUsers(tn->Path);
         FreeMem(tn);
         //if (MAIN.ThreadNotifiers.pFirst)
-            //pthread_spin_lock(&MAIN.csNotifier);
+            //laSpinLock(&MAIN.csNotifier);
     }
-    //pthread_spin_unlock(&MAIN.csNotifier);
+    //laSpinUnlock(&MAIN.csNotifier);
 
     while (1){
         if (MAIN.ReTriggerOperators) laSendOperatorTriggerEvent();

+ 29 - 2
la_util.c

@@ -1184,7 +1184,7 @@ void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
     laStaticMemoryPoolNode *smpn = smp->Pools.pFirst;
     void *ret;
 
-    //pthread_spin_lock(&smp->csMem);
+    //laSpinLock(&smp->csMem);
 
     if (!smpn || (smpn->UsedByte + size) > LA_MEMORY_POOL_128MB) smpn = memNewStaticPool(smp);
 
@@ -1192,7 +1192,7 @@ void *memStaticAcquireThread(laStaticMemoryPool *smp, int size){
 
     smpn->UsedByte += size;
 
-    //pthread_spin_unlock(&smp->csMem);
+    //laSpinUnlock(&smp->csMem);
 
     return ret;
 }
@@ -2197,4 +2197,31 @@ void usleep(unsigned int usec){
     WaitForSingleObject(timer, INFINITE);
     CloseHandle(timer);
 }
+
+void laSpinInit(SYSLOCK* lock) {
+    InitializeCriticalSection(lock);
+}
+void laSpinDestroy(SYSLOCK* lock) {
+    DeleteCriticalSection(lock);
+}
+void laSpinLock(SYSLOCK* lock) {
+    EnterCriticalSection(lock);
+}
+void laSpinUnlock(SYSLOCK* lock) {
+    LeaveCriticalSection(lock);
+}
 #endif
+#ifdef __linux__
+void laSpinInit(SYSLOCK* lock) {
+    laSpinInit(lock);
+}
+void laSpinDestroy(SYSLOCK* lock) {
+    laSpinDestroy(lock);
+}
+void laSpinLock(SYSLOCK* lock) {
+    pthread_spin_lock(lock);
+}
+void laSpinUnlock(SYSLOCK* lock) {
+    pthread_spin_unlock(lock);
+}
+#endif

+ 10 - 2
la_util.h

@@ -41,6 +41,7 @@
 #define SYSTEMDC DC
 #define SYSTEMRC RC
 #define SYSTEMDISPLAY Display
+#define SYSLOCK SYSLOCK
 #endif
 #ifdef _WIN32
 #include <Windows.h>
@@ -51,6 +52,7 @@
 #define SYSTEMRC HRC
 #define SYSTEMDISPLAY int
 #define PATH_MAX 4096
+#define SYSLOCK CRITICAL_SECTION
 #endif
 
 
@@ -343,7 +345,7 @@ STRUCTURE(laStaticMemoryPoolNode) {
 STRUCTURE(laStaticMemoryPool) {
 	int         EachSize;
 	laListHandle Pools;
-	//pthread_spinlock_t csMem;
+	//SYSLOCK csMem;
 };
 
 
@@ -710,4 +712,10 @@ void laOpenInternetLink(char* url);
 
 #ifdef _WIN32
 void usleep(unsigned int usec);
-#endif
+#endif
+
+
+void laSpinInit(SYSLOCK* lock);
+void laSpinDestroy(SYSLOCK * lock);
+void laSpinLock(SYSLOCK* lock);
+void laSpinUnlock(SYSLOCK* lock);

+ 2 - 2
resources/la_widgets.c

@@ -1573,7 +1573,7 @@ void la_ScopeDraw(laUiItem *ui, int h){
 
     tnsFlush(); tnsLineWidth(3.0);
 
-    pthread_spin_lock(&s->Lock);
+    laSpinLock(&s->Lock);
 
     real b1=s->Brightness1, b2=s->Brightness2; real hcy[3]; tnsRGB2HCY(lcolor,hcy); hcy[2]=0.75;
     real c1[3]; tnsHCY2RGB(hcy,c1);
@@ -1611,7 +1611,7 @@ void la_ScopeDraw(laUiItem *ui, int h){
     tnsColor4d(LA_COLOR3(c1),b2);
     tnsPackAs(GL_LINE_STRIP);
 
-    pthread_spin_unlock(&s->Lock);
+    laSpinUnlock(&s->Lock);
 
     tnsFlush(); tnsLineWidth(1.0);