-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsync.c
More file actions
2338 lines (1771 loc) · 61.2 KB
/
sync.c
File metadata and controls
2338 lines (1771 loc) · 61.2 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Direct port of and compatible with the cross platform 'sync' library: https://github.com/cubiclesoft/cross-platform-cpp
This source file is under the MIT license.
(C) 2016 CubicleSoft. All rights reserved.
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "zend_exceptions.h"
#include "ext/standard/info.h"
#if defined(PHP_WIN32) && PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION <= 4
#include "win32/php_stdint.h"
#else
#include <stdint.h>
#endif
/* Macros. Oh joy. */
#if PHP_MAJOR_VERSION >= 7
#include "zend_portability.h"
#endif
#include "php_sync.h"
#define PORTABLE_default_zend_object_name std
/* PHP_FE_END not defined in PHP < 5.3.7 */
#ifndef PHP_FE_END
#define PHP_FE_END {NULL, NULL, NULL}
#endif
/* For PHP 8 */
#ifndef TSRMLS_D
#define TSRMLS_D void
#define TSRMLS_DC
#define TSRMLS_C
#define TSRMLS_CC
#define TSRMLS_FETCH()
#endif
/* {{{ sync_module_entry
*/
zend_module_entry sync_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"sync",
NULL,
PHP_MINIT(sync),
PHP_MSHUTDOWN(sync),
NULL,
NULL,
PHP_MINFO(sync),
#if ZEND_MODULE_API_NO >= 20010901
PHP_SYNC_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SYNC
ZEND_GET_MODULE(sync)
#endif
#ifndef INFINITE
# define INFINITE 0xFFFFFFFF
#endif
/* Define some generic functions used several places. */
#if defined(PHP_WIN32)
/* Windows. */
sync_ThreadIDType sync_GetCurrentThreadID()
{
return GetCurrentThreadId();
}
uint64_t sync_GetUnixMicrosecondTime()
{
FILETIME TempTime;
ULARGE_INTEGER TempTime2;
uint64_t Result;
GetSystemTimeAsFileTime(&TempTime);
TempTime2.HighPart = TempTime.dwHighDateTime;
TempTime2.LowPart = TempTime.dwLowDateTime;
Result = TempTime2.QuadPart;
Result = (Result / 10) - (uint64_t)11644473600000000ULL;
return Result;
}
#else
/* POSIX pthreads. */
sync_ThreadIDType sync_GetCurrentThreadID()
{
return pthread_self();
}
uint64_t sync_GetUnixMicrosecondTime()
{
struct timeval TempTime;
if (gettimeofday(&TempTime, NULL)) return 0;
return (uint64_t)((uint64_t)TempTime.tv_sec * (uint64_t)1000000 + (uint64_t)TempTime.tv_usec);
}
/* Dear Apple: You hire plenty of developers, so please fix your OS. */
int sync_CSGX__ClockGetTimeRealtime(struct timespec *ts)
{
#ifdef __APPLE__
clock_serv_t cclock;
mach_timespec_t mts;
if (host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock) != KERN_SUCCESS) return -1;
if (clock_get_time(cclock, &mts) != KERN_SUCCESS) return -1;
if (mach_port_deallocate(mach_task_self(), cclock) != KERN_SUCCESS) return -1;
ts->tv_sec = mts.tv_sec;
ts->tv_nsec = mts.tv_nsec;
return 0;
#else
return clock_gettime(CLOCK_REALTIME, ts);
#endif
}
size_t sync_GetUnixSystemAlignmentSize()
{
struct {
int MxInt;
} x;
struct {
int MxInt;
char MxChar;
} y;
return sizeof(y) - sizeof(x);
}
size_t sync_AlignUnixSize(size_t Size)
{
size_t AlignSize = sync_GetUnixSystemAlignmentSize();
if (Size % AlignSize) Size += AlignSize - (Size % AlignSize);
return Size;
}
int sync_InitUnixNamedMem(char **ResultMem, size_t *StartPos, const char *Prefix, const char *Name, size_t Size)
{
int Result = -1;
*ResultMem = NULL;
*StartPos = (Name != NULL ? sync_AlignUnixSize(1) + sync_AlignUnixSize(sizeof(pthread_mutex_t)) + sync_AlignUnixSize(sizeof(uint32_t)) : 0);
/* First byte indicates initialization status (0 = completely uninitialized, 1 = first mutex initialized, 2 = ready). */
/* Next few bytes are a shared mutex object. */
/* Size bytes follow for whatever. */
Size += *StartPos;
Size = sync_AlignUnixSize(Size);
if (Name == NULL)
{
*ResultMem = (char *)ecalloc(1, Size);
Result = 0;
}
else
{
/* Deal with really small name limits with a pseudo-hash. */
char Name2[SHM_NAME_MAX], Nums[50];
size_t x, x2 = 0, y = strlen(Prefix), z = 0;
memset(Name2, 0, sizeof(Name2));
for (x = 0; x < y; x++)
{
Name2[x2] = (char)(((unsigned int)(unsigned char)Name2[x2]) * 37 + ((unsigned int)(unsigned char)Prefix[x]));
x2++;
if (x2 == sizeof(Name2) - 1)
{
x2 = 1;
z++;
}
}
sprintf(Nums, "-%u-%u-", (unsigned int)sync_GetUnixSystemAlignmentSize(), (unsigned int)Size);
y = strlen(Nums);
for (x = 0; x < y; x++)
{
Name2[x2] = (char)(((unsigned int)(unsigned char)Name2[x2]) * 37 + ((unsigned int)(unsigned char)Nums[x]));
x2++;
if (x2 == sizeof(Name2) - 1)
{
x2 = 1;
z++;
}
}
y = strlen(Name);
for (x = 0; x < y; x++)
{
Name2[x2] = (char)(((unsigned int)(unsigned char)Name2[x2]) * 37 + ((unsigned int)(unsigned char)Name[x]));
x2++;
if (x2 == sizeof(Name2) - 1)
{
x2 = 1;
z++;
}
}
/* Normalize the alphabet if it looped. */
if (z)
{
unsigned char TempChr;
y = (z > 1 ? sizeof(Name2) - 1 : x2);
for (x = 1; x < y; x++)
{
TempChr = ((unsigned char)Name2[x]) & 0x3F;
if (TempChr < 10) TempChr += '0';
else if (TempChr < 36) TempChr = TempChr - 10 + 'A';
else if (TempChr < 62) TempChr = TempChr - 36 + 'a';
else if (TempChr == 62) TempChr = '_';
else TempChr = '-';
Name2[x] = (char)TempChr;
}
}
for (x = 1; x < sizeof(Name2) && Name2[x]; x++)
{
if (Name2[x] == '\\' || Name2[x] == '/') Name2[x] = '_';
}
pthread_mutex_t *MutexPtr;
uint32_t *RefCountPtr;
/* Attempt to create the named shared memory object. */
mode_t PrevMask = umask(0);
int fp = shm_open(Name2, O_RDWR | O_CREAT | O_EXCL, 0666);
if (fp > -1)
{
/* Ignore platform errors (for now). */
while (ftruncate(fp, Size) < 0 && errno == EINTR)
{
}
*ResultMem = (char *)mmap(NULL, Size, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0);
if ((*ResultMem) == MAP_FAILED) *ResultMem = NULL;
else
{
pthread_mutexattr_t MutexAttr;
pthread_mutexattr_init(&MutexAttr);
pthread_mutexattr_setpshared(&MutexAttr, PTHREAD_PROCESS_SHARED);
MutexPtr = (pthread_mutex_t *)((*ResultMem) + sync_AlignUnixSize(1));
RefCountPtr = (uint32_t *)((*ResultMem) + sync_AlignUnixSize(1) + sync_AlignUnixSize(sizeof(pthread_mutex_t)));
pthread_mutex_init(MutexPtr, &MutexAttr);
pthread_mutex_lock(MutexPtr);
(*ResultMem)[0] = '\x01';
RefCountPtr[0] = 1;
Result = 0;
}
close(fp);
}
else
{
/* Attempt to open the named shared memory object. */
fp = shm_open(Name2, O_RDWR, 0666);
if (fp > -1)
{
/* Ignore platform errors (for now). */
while (ftruncate(fp, Size) < 0 && errno == EINTR)
{
}
*ResultMem = (char *)mmap(NULL, Size, PROT_READ | PROT_WRITE, MAP_SHARED, fp, 0);
if (*ResultMem == MAP_FAILED) ResultMem = NULL;
else
{
/* Wait until the space is fully initialized. */
if ((*ResultMem)[0] == '\x00')
{
while ((*ResultMem)[0] == '\x00')
{
usleep(2000);
}
}
char *MemPtr = (*ResultMem) + sync_AlignUnixSize(1);
MutexPtr = (pthread_mutex_t *)(MemPtr);
MemPtr += sync_AlignUnixSize(sizeof(pthread_mutex_t));
RefCountPtr = (uint32_t *)(MemPtr);
MemPtr += sync_AlignUnixSize(sizeof(uint32_t));
pthread_mutex_lock(MutexPtr);
if (RefCountPtr[0]) Result = 1;
else
{
/* If this is the first reference, reset the RAM to 0's for platform consistency to force a rebuild of the object. */
memset(MemPtr, 0, Size);
Result = 0;
}
RefCountPtr[0]++;
pthread_mutex_unlock(MutexPtr);
}
close(fp);
}
}
umask(PrevMask);
}
return Result;
}
void sync_UnixNamedMemReady(char *MemPtr)
{
pthread_mutex_unlock((pthread_mutex_t *)(MemPtr + sync_AlignUnixSize(1)));
}
void sync_UnmapUnixNamedMem(char *MemPtr, size_t Size)
{
pthread_mutex_t *MutexPtr;
uint32_t *RefCountPtr;
char *MemPtr2 = MemPtr + sync_AlignUnixSize(1);
MutexPtr = (pthread_mutex_t *)(MemPtr2);
MemPtr2 += sync_AlignUnixSize(sizeof(pthread_mutex_t));
RefCountPtr = (uint32_t *)(MemPtr2);
pthread_mutex_lock(MutexPtr);
if (RefCountPtr[0]) RefCountPtr[0]--;
pthread_mutex_unlock(MutexPtr);
munmap(MemPtr, sync_AlignUnixSize(1) + sync_AlignUnixSize(sizeof(pthread_mutex_t)) + sync_AlignUnixSize(sizeof(uint32_t)) + Size);
}
/* Basic *NIX Semaphore functions. */
size_t sync_GetUnixSemaphoreSize()
{
return sync_AlignUnixSize(sizeof(pthread_mutex_t)) + sync_AlignUnixSize(sizeof(uint32_t)) + sync_AlignUnixSize(sizeof(uint32_t)) + sync_AlignUnixSize(sizeof(pthread_cond_t));
}
void sync_GetUnixSemaphore(sync_UnixSemaphoreWrapper *Result, char *Mem)
{
Result->MxMutex = (pthread_mutex_t *)(Mem);
Mem += sync_AlignUnixSize(sizeof(pthread_mutex_t));
Result->MxCount = (uint32_t *)(Mem);
Mem += sync_AlignUnixSize(sizeof(uint32_t));
Result->MxMax = (uint32_t *)(Mem);
Mem += sync_AlignUnixSize(sizeof(uint32_t));
Result->MxCond = (pthread_cond_t *)(Mem);
}
void sync_InitUnixSemaphore(sync_UnixSemaphoreWrapper *UnixSemaphore, int Shared, uint32_t Start, uint32_t Max)
{
pthread_mutexattr_t MutexAttr;
pthread_condattr_t CondAttr;
pthread_mutexattr_init(&MutexAttr);
pthread_condattr_init(&CondAttr);
if (Shared)
{
pthread_mutexattr_setpshared(&MutexAttr, PTHREAD_PROCESS_SHARED);
pthread_condattr_setpshared(&CondAttr, PTHREAD_PROCESS_SHARED);
}
pthread_mutex_init(UnixSemaphore->MxMutex, &MutexAttr);
if (Start > Max) Start = Max;
UnixSemaphore->MxCount[0] = Start;
UnixSemaphore->MxMax[0] = Max;
pthread_cond_init(UnixSemaphore->MxCond, &CondAttr);
pthread_condattr_destroy(&CondAttr);
pthread_mutexattr_destroy(&MutexAttr);
}
int sync_WaitForUnixSemaphore(sync_UnixSemaphoreWrapper *UnixSemaphore, uint32_t Wait)
{
if (Wait == 0)
{
/* Avoid the scenario of deadlock on the semaphore itself for 0 wait. */
if (pthread_mutex_trylock(UnixSemaphore->MxMutex) != 0) return 0;
}
else
{
if (pthread_mutex_lock(UnixSemaphore->MxMutex) != 0) return 0;
}
int Result = 0;
if (UnixSemaphore->MxCount[0])
{
UnixSemaphore->MxCount[0]--;
Result = 1;
}
else if (Wait == INFINITE)
{
int Result2;
do
{
Result2 = pthread_cond_wait(UnixSemaphore->MxCond, UnixSemaphore->MxMutex);
if (Result2 != 0) break;
} while (!UnixSemaphore->MxCount[0]);
if (Result2 == 0)
{
UnixSemaphore->MxCount[0]--;
Result = 1;
}
}
else if (Wait == 0)
{
/* Failed to obtain lock. Nothing to do. */
}
else
{
struct timespec TempTime;
if (sync_CSGX__ClockGetTimeRealtime(&TempTime) == -1) return 0;
TempTime.tv_sec += Wait / 1000;
TempTime.tv_nsec += (Wait % 1000) * 1000000;
TempTime.tv_sec += TempTime.tv_nsec / 1000000000;
TempTime.tv_nsec = TempTime.tv_nsec % 1000000000;
int Result2;
do
{
/* Some platforms have pthread_cond_timedwait() but not pthread_mutex_timedlock() or sem_timedwait() (e.g. Mac OSX). */
Result2 = pthread_cond_timedwait(UnixSemaphore->MxCond, UnixSemaphore->MxMutex, &TempTime);
if (Result2 != 0) break;
} while (!UnixSemaphore->MxCount[0]);
if (Result2 == 0)
{
UnixSemaphore->MxCount[0]--;
Result = 1;
}
}
pthread_mutex_unlock(UnixSemaphore->MxMutex);
return Result;
}
int sync_ReleaseUnixSemaphore(sync_UnixSemaphoreWrapper *UnixSemaphore, uint32_t *PrevVal)
{
if (pthread_mutex_lock(UnixSemaphore->MxMutex) != 0) return 0;
if (PrevVal != NULL) *PrevVal = UnixSemaphore->MxCount[0];
UnixSemaphore->MxCount[0]++;
if (UnixSemaphore->MxCount[0] > UnixSemaphore->MxMax[0]) UnixSemaphore->MxCount[0] = UnixSemaphore->MxMax[0];
/* Let a waiting thread have at it. */
pthread_cond_signal(UnixSemaphore->MxCond);
pthread_mutex_unlock(UnixSemaphore->MxMutex);
return 1;
}
void sync_FreeUnixSemaphore(sync_UnixSemaphoreWrapper *UnixSemaphore)
{
pthread_mutex_destroy(UnixSemaphore->MxMutex);
pthread_cond_destroy(UnixSemaphore->MxCond);
}
/* Basic *NIX Event functions. */
size_t sync_GetUnixEventSize()
{
return sync_AlignUnixSize(sizeof(pthread_mutex_t)) + sync_AlignUnixSize(2) + sync_AlignUnixSize(sizeof(uint32_t)) + sync_AlignUnixSize(sizeof(pthread_cond_t));
}
void sync_GetUnixEvent(sync_UnixEventWrapper *Result, char *Mem)
{
Result->MxMutex = (pthread_mutex_t *)(Mem);
Mem += sync_AlignUnixSize(sizeof(pthread_mutex_t));
Result->MxManual = Mem;
Result->MxSignaled = Mem + 1;
Mem += sync_AlignUnixSize(2);
Result->MxWaiting = (uint32_t *)(Mem);
Mem += sync_AlignUnixSize(sizeof(uint32_t));
Result->MxCond = (pthread_cond_t *)(Mem);
}
void sync_InitUnixEvent(sync_UnixEventWrapper *UnixEvent, int Shared, int Manual, int Signaled)
{
pthread_mutexattr_t MutexAttr;
pthread_condattr_t CondAttr;
pthread_mutexattr_init(&MutexAttr);
pthread_condattr_init(&CondAttr);
if (Shared)
{
pthread_mutexattr_setpshared(&MutexAttr, PTHREAD_PROCESS_SHARED);
pthread_condattr_setpshared(&CondAttr, PTHREAD_PROCESS_SHARED);
}
pthread_mutex_init(UnixEvent->MxMutex, &MutexAttr);
UnixEvent->MxManual[0] = (Manual ? '\x01' : '\x00');
UnixEvent->MxSignaled[0] = (Signaled ? '\x01' : '\x00');
UnixEvent->MxWaiting[0] = 0;
pthread_cond_init(UnixEvent->MxCond, &CondAttr);
pthread_condattr_destroy(&CondAttr);
pthread_mutexattr_destroy(&MutexAttr);
}
int sync_WaitForUnixEvent(sync_UnixEventWrapper *UnixEvent, uint32_t Wait)
{
if (Wait == 0)
{
/* Avoid the scenario of deadlock on the semaphore itself for 0 wait. */
if (pthread_mutex_trylock(UnixEvent->MxMutex) != 0) return 0;
}
else
{
if (pthread_mutex_lock(UnixEvent->MxMutex) != 0) return 0;
}
int Result = 0;
/* Avoid a potential starvation issue by only allowing signaled manual events OR if there are no other waiting threads. */
if (UnixEvent->MxSignaled[0] != '\x00' && (UnixEvent->MxManual[0] != '\x00' || !UnixEvent->MxWaiting[0]))
{
/* Reset auto events. */
if (UnixEvent->MxManual[0] == '\x00') UnixEvent->MxSignaled[0] = '\x00';
Result = 1;
}
else if (Wait == INFINITE)
{
UnixEvent->MxWaiting[0]++;
int Result2;
do
{
Result2 = pthread_cond_wait(UnixEvent->MxCond, UnixEvent->MxMutex);
if (Result2 != 0) break;
} while (UnixEvent->MxSignaled[0] == '\x00');
UnixEvent->MxWaiting[0]--;
if (Result2 == 0)
{
/* Reset auto events. */
if (UnixEvent->MxManual[0] == '\x00') UnixEvent->MxSignaled[0] = '\x00';
Result = 1;
}
}
else if (Wait == 0)
{
/* Failed to obtain lock. Nothing to do. */
}
else
{
struct timespec TempTime;
if (sync_CSGX__ClockGetTimeRealtime(&TempTime) == -1)
{
pthread_mutex_unlock(UnixEvent->MxMutex);
return 0;
}
TempTime.tv_sec += Wait / 1000;
TempTime.tv_nsec += (Wait % 1000) * 1000000;
TempTime.tv_sec += TempTime.tv_nsec / 1000000000;
TempTime.tv_nsec = TempTime.tv_nsec % 1000000000;
UnixEvent->MxWaiting[0]++;
int Result2;
do
{
/* Some platforms have pthread_cond_timedwait() but not pthread_mutex_timedlock() or sem_timedwait() (e.g. Mac OSX). */
Result2 = pthread_cond_timedwait(UnixEvent->MxCond, UnixEvent->MxMutex, &TempTime);
if (Result2 != 0) break;
} while (UnixEvent->MxSignaled[0] == '\x00');
UnixEvent->MxWaiting[0]--;
if (Result2 == 0)
{
/* Reset auto events. */
if (UnixEvent->MxManual[0] == '\x00') UnixEvent->MxSignaled[0] = '\x00';
Result = 1;
}
}
pthread_mutex_unlock(UnixEvent->MxMutex);
return Result;
}
int sync_FireUnixEvent(sync_UnixEventWrapper *UnixEvent)
{
if (pthread_mutex_lock(UnixEvent->MxMutex) != 0) return 0;
UnixEvent->MxSignaled[0] = '\x01';
/* Let all waiting threads through for manual events, otherwise just one waiting thread (if any). */
if (UnixEvent->MxManual[0] != '\x00') pthread_cond_broadcast(UnixEvent->MxCond);
else pthread_cond_signal(UnixEvent->MxCond);
pthread_mutex_unlock(UnixEvent->MxMutex);
return 1;
}
/* Only call for manual events. */
int sync_ResetUnixEvent(sync_UnixEventWrapper *UnixEvent)
{
if (UnixEvent->MxManual[0] == '\x00') return 0;
if (pthread_mutex_lock(UnixEvent->MxMutex) != 0) return 0;
UnixEvent->MxSignaled[0] = '\x00';
pthread_mutex_unlock(UnixEvent->MxMutex);
return 1;
}
void sync_FreeUnixEvent(sync_UnixEventWrapper *UnixEvent)
{
pthread_mutex_destroy(UnixEvent->MxMutex);
pthread_cond_destroy(UnixEvent->MxCond);
}
#endif
/* {{{ PHP 5 and 7 crossover compatibility defines and functions */
#if PHP_MAJOR_VERSION >= 7
#define PORTABLE_new_zend_object_func(funcname) zend_object *funcname(zend_class_entry *ce)
#define PORTABLE_new_zend_object_return_var
#define PORTABLE_new_zend_object_return_var_ref NULL
#define PORTABLE_allocate_zend_object(size, ce) ecalloc(1, (size) + zend_object_properties_size(ce));
#define PORTABLE_new_zend_object_return(std) return (std)
void PORTABLE_InitZendObject(void *obj, zend_object *std, void *unused1, void *FreeFunc, zend_object_handlers *Handlers, zend_class_entry *ce TSRMLS_DC)
{
/* Initialize Zend. */
zend_object_std_init(std, ce TSRMLS_CC);
object_properties_init(std, ce);
/* Initialize destructor callback. */
std->handlers = Handlers;
}
typedef zend_long PORTABLE_ZPP_ARG_long;
typedef size_t PORTABLE_ZPP_ARG_size;
typedef zval * PORTABLE_ZPP_ARG_zval_ref;
#define PORTABLE_ZPP_ARG_zval_ref_deref(var) var
static inline void *PHP_7_zend_object_to_object(zend_object *std)
{
return (void *)((char *)std - std->handlers->offset);
}
#define PORTABLE_zend_object_store_get_object() PHP_7_zend_object_to_object(Z_OBJ_P(getThis()))
#define PORTABLE_free_zend_object_func(funcname) void funcname(zend_object *object)
#define PORTABLE_free_zend_object_get_object(object) PHP_7_zend_object_to_object(object)
#define PORTABLE_free_zend_object_free_object(obj) zend_object_std_dtor(&obj->std);
#define PORTABLE_RETURN_STRINGL(str, len) RETURN_STRINGL(str, len)
#else
#define PORTABLE_new_zend_object_func(funcname) zend_object_value funcname(zend_class_entry *ce TSRMLS_DC)
#define PORTABLE_new_zend_object_return_var zend_object_value retval
#define PORTABLE_new_zend_object_return_var_ref &retval
#define PORTABLE_new_zend_object_return(std) return retval
#define PORTABLE_allocate_zend_object(size, ce) ecalloc(1, (size));
void PORTABLE_InitZendObject(void *obj, zend_object *std, zend_object_value *retval, zend_objects_free_object_storage_t FreeFunc, zend_object_handlers *Handlers, zend_class_entry *ce TSRMLS_DC)
{
#if PHP_VERSION_ID < 50399
zval *tmp;
#endif
/* Initialize Zend. */
zend_object_std_init(std, ce TSRMLS_CC);
#if PHP_VERSION_ID < 50399
zend_hash_copy(std->properties, &ce->default_properties, (copy_ctor_func_t)zval_add_ref, (void *)&tmp, sizeof(zval *));
#else
object_properties_init(std, ce);
#endif
/* Initialize destructor callback. */
retval->handle = zend_objects_store_put(obj, (zend_objects_store_dtor_t)zend_objects_destroy_object, FreeFunc, NULL TSRMLS_CC);
retval->handlers = Handlers;
}
typedef long PORTABLE_ZPP_ARG_long;
typedef int PORTABLE_ZPP_ARG_size;
typedef zval ** PORTABLE_ZPP_ARG_zval_ref;
#define PORTABLE_ZPP_ARG_zval_ref_deref(var) *var
#define PORTABLE_zend_object_store_get_object() zend_object_store_get_object(getThis() TSRMLS_CC)
#define PORTABLE_free_zend_object_func(funcname) void funcname(void *object TSRMLS_DC)
#define PORTABLE_free_zend_object_get_object(object) object;
#define PORTABLE_free_zend_object_free_object(obj) zend_object_std_dtor(&obj->std TSRMLS_CC); efree(obj);
#define PORTABLE_RETURN_STRINGL(str, len) RETURN_STRINGL(str, len, 1)
#endif
/* }}} */
/* Mutex */
PHP_SYNC_API zend_class_entry *sync_Mutex_ce;
static zend_object_handlers sync_Mutex_object_handlers;
PORTABLE_free_zend_object_func(sync_Mutex_free_object);
/* {{{ Initialize internal Mutex structure. */
PORTABLE_new_zend_object_func(sync_Mutex_create_object)
{
PORTABLE_new_zend_object_return_var;
sync_Mutex_object *obj;
/* Create the object. */
obj = (sync_Mutex_object *)PORTABLE_allocate_zend_object(sizeof(sync_Mutex_object), ce);
PORTABLE_InitZendObject(obj, &obj->std, PORTABLE_new_zend_object_return_var_ref, sync_Mutex_free_object, &sync_Mutex_object_handlers, ce TSRMLS_CC);
/* Initialize Mutex information. */
#if defined(PHP_WIN32)
obj->MxWinMutex = NULL;
InitializeCriticalSection(&obj->MxWinCritSection);
#else
obj->MxNamed = 0;
obj->MxMem = NULL;
pthread_mutex_init(&obj->MxPthreadCritSection, NULL);
#endif
obj->MxOwnerID = 0;
obj->MxCount = 0;
PORTABLE_new_zend_object_return(&obj->std);
}
/* }}} */
/* {{{ Unlocks a mutex. */
int sync_Mutex_unlock_internal(sync_Mutex_object *obj, int all)
{
#if defined(PHP_WIN32)
EnterCriticalSection(&obj->MxWinCritSection);
/* Make sure the mutex exists and make sure it is owned by the calling thread. */
if (obj->MxWinMutex == NULL || obj->MxOwnerID != sync_GetCurrentThreadID())
{
LeaveCriticalSection(&obj->MxWinCritSection);
return 0;
}
if (all) obj->MxCount = 1;
obj->MxCount--;
if (!obj->MxCount)
{
obj->MxOwnerID = 0;
/* Release the mutex. */
ReleaseMutex(obj->MxWinMutex);
}
LeaveCriticalSection(&obj->MxWinCritSection);
#else
if (pthread_mutex_lock(&obj->MxPthreadCritSection) != 0) return 0;
/* Make sure the mutex exists and make sure it is owned by the calling thread. */
if (obj->MxMem == NULL || obj->MxOwnerID != sync_GetCurrentThreadID())
{
pthread_mutex_unlock(&obj->MxPthreadCritSection);
return 0;
}
if (all) obj->MxCount = 1;
obj->MxCount--;
if (!obj->MxCount)
{
obj->MxOwnerID = 0;
/* Release the mutex. */
sync_ReleaseUnixSemaphore(&obj->MxPthreadMutex, NULL);
}
pthread_mutex_unlock(&obj->MxPthreadCritSection);
#endif
return 1;
}
/* }}} */
/* {{{ Free internal Mutex structure. */
PORTABLE_free_zend_object_func(sync_Mutex_free_object)
{
sync_Mutex_object *obj = (sync_Mutex_object *)PORTABLE_free_zend_object_get_object(object);
sync_Mutex_unlock_internal(obj, 1);
#if defined(PHP_WIN32)
if (obj->MxWinMutex != NULL) CloseHandle(obj->MxWinMutex);
DeleteCriticalSection(&obj->MxWinCritSection);
#else
if (obj->MxMem != NULL)
{
if (obj->MxNamed) sync_UnmapUnixNamedMem(obj->MxMem, sync_GetUnixSemaphoreSize());
else
{
sync_FreeUnixSemaphore(&obj->MxPthreadMutex);
efree(obj->MxMem);
}
}
pthread_mutex_destroy(&obj->MxPthreadCritSection);
#endif
PORTABLE_free_zend_object_free_object(obj);
}
/* }}} */
/* {{{ proto void Sync_Mutex::__construct([string $name = null])
Constructs a named or unnamed mutex object. */
PHP_METHOD(sync_Mutex, __construct)
{
char *name = NULL;
PORTABLE_ZPP_ARG_size name_len;
sync_Mutex_object *obj;
#if defined(PHP_WIN32)
SECURITY_ATTRIBUTES SecAttr;
#else
size_t Pos, TempSize;
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &name, &name_len) == FAILURE) return;
obj = (sync_Mutex_object *)PORTABLE_zend_object_store_get_object();
if (name_len < 1) name = NULL;
#if defined(PHP_WIN32)
SecAttr.nLength = sizeof(SecAttr);
SecAttr.lpSecurityDescriptor = NULL;
SecAttr.bInheritHandle = TRUE;
obj->MxWinMutex = CreateMutexA(&SecAttr, FALSE, name);
if (obj->MxWinMutex == NULL)
{
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Mutex could not be created", 0 TSRMLS_CC);
return;
}
#else
TempSize = sync_GetUnixSemaphoreSize();
obj->MxNamed = (name != NULL ? 1 : 0);
int Result = sync_InitUnixNamedMem(&obj->MxMem, &Pos, "/Sync_Mutex", name, TempSize);
if (Result < 0)
{
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Mutex could not be created", 0 TSRMLS_CC);
return;
}
sync_GetUnixSemaphore(&obj->MxPthreadMutex, obj->MxMem + Pos);
/* Handle the first time this mutex has been opened. */
if (Result == 0)
{
sync_InitUnixSemaphore(&obj->MxPthreadMutex, obj->MxNamed, 1, 1);
if (obj->MxNamed) sync_UnixNamedMemReady(obj->MxMem);
}
#endif
}
/* }}} */
/* {{{ proto bool Sync_Mutex::lock([int $wait = -1])
Locks a mutex object. */
PHP_METHOD(sync_Mutex, lock)
{
PORTABLE_ZPP_ARG_long wait = -1;
sync_Mutex_object *obj;
#if defined(PHP_WIN32)
DWORD Result;
#else
#endif
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &wait) == FAILURE) return;
obj = (sync_Mutex_object *)PORTABLE_zend_object_store_get_object();
#if defined(PHP_WIN32)
EnterCriticalSection(&obj->MxWinCritSection);
/* Check to see if this is already owned by the calling thread. */
if (obj->MxOwnerID == sync_GetCurrentThreadID())
{
obj->MxCount++;
LeaveCriticalSection(&obj->MxWinCritSection);
RETURN_TRUE;
}
LeaveCriticalSection(&obj->MxWinCritSection);
/* Acquire the mutex. */
Result = WaitForSingleObject(obj->MxWinMutex, (DWORD)(wait > -1 ? wait : INFINITE));
if (Result != WAIT_OBJECT_0) RETURN_FALSE;
EnterCriticalSection(&obj->MxWinCritSection);
obj->MxOwnerID = sync_GetCurrentThreadID();
obj->MxCount = 1;
LeaveCriticalSection(&obj->MxWinCritSection);
#else
if (pthread_mutex_lock(&obj->MxPthreadCritSection) != 0)
{
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Unable to acquire mutex critical section", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* Check to see if this mutex is already owned by the calling thread. */
if (obj->MxOwnerID == sync_GetCurrentThreadID())
{
obj->MxCount++;
pthread_mutex_unlock(&obj->MxPthreadCritSection);
RETURN_TRUE;
}
pthread_mutex_unlock(&obj->MxPthreadCritSection);
if (!sync_WaitForUnixSemaphore(&obj->MxPthreadMutex, (uint32_t)(wait > -1 ? wait : INFINITE))) RETURN_FALSE;
pthread_mutex_lock(&obj->MxPthreadCritSection);
obj->MxOwnerID = sync_GetCurrentThreadID();
obj->MxCount = 1;
pthread_mutex_unlock(&obj->MxPthreadCritSection);
#endif