@@ -48,6 +48,29 @@ static const char *g_shared_model;
48
48
/* pointer to globals allocated in SHM and shared across processes */
49
49
zend_smm_shared_globals * smm_shared_globals ;
50
50
51
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
52
+
53
+ #include <pthread.h>
54
+
55
+ /**
56
+ * This struct is allocated from shared memory and contains a
57
+ * PTHREAD_PROCESS_SHARED mutex.
58
+ */
59
+ struct opcache_locks {
60
+ /**
61
+ * The mutex for zend_shared_alloc_lock().
62
+ */
63
+ pthread_mutex_t alloc ;
64
+ };
65
+
66
+ /**
67
+ * Note, this variable should not be put in struct zend_accel_globals
68
+ * because this is not a per-thread variable.
69
+ */
70
+ static struct opcache_locks * opcache_locks ;
71
+
72
+ #endif // HAVE_PTHREAD_PROCESS_SHARED
73
+
51
74
#ifndef ZEND_WIN32
52
75
#ifdef ZTS
53
76
static MUTEX_T zts_lock ;
@@ -75,12 +98,24 @@ static const zend_shared_memory_handler_entry handler_table[] = {
75
98
#ifndef ZEND_WIN32
76
99
void zend_shared_alloc_create_lock (char * lockfile_path )
77
100
{
78
- int val ;
79
-
80
101
#ifdef ZTS
81
102
zts_lock = tsrm_mutex_alloc ();
82
103
#endif
83
104
105
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
106
+ opcache_locks = mmap (NULL , sizeof (* opcache_locks ),
107
+ PROT_READ |PROT_WRITE ,
108
+ MAP_SHARED |MAP_ANONYMOUS , -1 , 0 );
109
+ if (opcache_locks == MAP_FAILED ) {
110
+ zend_accel_error_noreturn (ACCEL_LOG_FATAL , "Unable to create opcache_locks: %s (%d)" , strerror (errno ), errno );
111
+ }
112
+
113
+ pthread_mutexattr_t mutexattr ;
114
+ pthread_mutexattr_init (& mutexattr );
115
+ pthread_mutexattr_setpshared (& mutexattr , PTHREAD_PROCESS_SHARED );
116
+ pthread_mutex_init (& opcache_locks -> alloc , & mutexattr );
117
+ #endif // HAVE_PTHREAD_PROCESS_SHARED
118
+
84
119
snprintf (lockfile_name , sizeof (lockfile_name ), "%s/%sXXXXXX" , lockfile_path , SEM_FILENAME_PREFIX );
85
120
lock_file = mkstemp (lockfile_name );
86
121
if (lock_file == -1 ) {
@@ -89,7 +124,7 @@ void zend_shared_alloc_create_lock(char *lockfile_path)
89
124
90
125
fchmod (lock_file , 0666 );
91
126
92
- val = fcntl (lock_file , F_GETFD , 0 );
127
+ int val = fcntl (lock_file , F_GETFD , 0 );
93
128
val |= FD_CLOEXEC ;
94
129
fcntl (lock_file , F_SETFD , val );
95
130
@@ -301,6 +336,12 @@ void zend_shared_alloc_shutdown(void)
301
336
}
302
337
ZSMMG (shared_segments ) = NULL ;
303
338
g_shared_alloc_handler = NULL ;
339
+
340
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
341
+ pthread_mutex_destroy (& opcache_locks -> alloc );
342
+ munmap (opcache_locks , sizeof (opcache_locks ));
343
+ #endif
344
+
304
345
#ifndef ZEND_WIN32
305
346
close (lock_file );
306
347
@@ -452,18 +493,20 @@ void zend_shared_alloc_lock(void)
452
493
{
453
494
ZEND_ASSERT (!ZCG (locked ));
454
495
455
- #ifndef ZEND_WIN32
496
+ #if !defined(ZEND_WIN32 ) && defined(ZTS )
497
+ tsrm_mutex_lock (zts_lock );
498
+ #endif
499
+
500
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
501
+ pthread_mutex_lock (& opcache_locks -> alloc );
502
+ #elif !defined(ZEND_WIN32 )
456
503
struct flock mem_write_lock ;
457
504
458
505
mem_write_lock .l_type = F_WRLCK ;
459
506
mem_write_lock .l_whence = SEEK_SET ;
460
507
mem_write_lock .l_start = 0 ;
461
508
mem_write_lock .l_len = 1 ;
462
509
463
- #ifdef ZTS
464
- tsrm_mutex_lock (zts_lock );
465
- #endif
466
-
467
510
#if 0
468
511
/* this will happen once per process, and will un-globalize mem_write_lock */
469
512
if (mem_write_lock .l_pid == -1 ) {
@@ -491,7 +534,7 @@ void zend_shared_alloc_unlock(void)
491
534
{
492
535
ZEND_ASSERT (ZCG (locked ));
493
536
494
- #ifndef ZEND_WIN32
537
+ #if !defined( ZEND_WIN32 ) && !defined( HAVE_PTHREAD_PROCESS_SHARED )
495
538
struct flock mem_write_unlock ;
496
539
497
540
mem_write_unlock .l_type = F_UNLCK ;
@@ -502,16 +545,19 @@ void zend_shared_alloc_unlock(void)
502
545
503
546
ZCG (locked ) = 0 ;
504
547
505
- #ifndef ZEND_WIN32
548
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
549
+ pthread_mutex_unlock (& opcache_locks -> alloc );
550
+ #elif !defined(ZEND_WIN32 )
506
551
if (fcntl (lock_file , F_SETLK , & mem_write_unlock ) == -1 ) {
507
552
zend_accel_error_noreturn (ACCEL_LOG_ERROR , "Cannot remove lock - %s (%d)" , strerror (errno ), errno );
508
553
}
509
- #ifdef ZTS
510
- tsrm_mutex_unlock (zts_lock );
511
- #endif
512
554
#else
513
555
zend_shared_alloc_unlock_win32 ();
514
556
#endif
557
+
558
+ #if !defined(ZEND_WIN32 ) && defined(ZTS )
559
+ tsrm_mutex_unlock (zts_lock );
560
+ #endif
515
561
}
516
562
517
563
void zend_shared_alloc_init_xlat_table (void )
0 commit comments