@@ -55,6 +55,29 @@ static const char *g_shared_model;
55
55
/* pointer to globals allocated in SHM and shared across processes */
56
56
zend_smm_shared_globals * smm_shared_globals ;
57
57
58
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
59
+
60
+ #include <pthread.h>
61
+
62
+ /**
63
+ * This struct is allocated from shared memory and contains a
64
+ * PTHREAD_PROCESS_SHARED mutex.
65
+ */
66
+ struct opcache_locks {
67
+ /**
68
+ * The mutex for zend_shared_alloc_lock().
69
+ */
70
+ pthread_mutex_t alloc ;
71
+ };
72
+
73
+ /**
74
+ * Note, this variable should not be put in struct zend_accel_globals
75
+ * because this is not a per-thread variable.
76
+ */
77
+ static struct opcache_locks * opcache_locks ;
78
+
79
+ #endif // HAVE_PTHREAD_PROCESS_SHARED
80
+
58
81
#ifndef ZEND_WIN32
59
82
#ifdef ZTS
60
83
static MUTEX_T zts_lock ;
@@ -82,12 +105,24 @@ static const zend_shared_memory_handler_entry handler_table[] = {
82
105
#ifndef ZEND_WIN32
83
106
void zend_shared_alloc_create_lock (char * lockfile_path )
84
107
{
85
- int val ;
86
-
87
108
#ifdef ZTS
88
109
zts_lock = tsrm_mutex_alloc ();
89
110
#endif
90
111
112
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
113
+ opcache_locks = mmap (NULL , sizeof (* opcache_locks ),
114
+ PROT_READ |PROT_WRITE ,
115
+ MAP_SHARED |MAP_ANONYMOUS , -1 , 0 );
116
+ if (opcache_locks == MAP_FAILED ) {
117
+ zend_accel_error_noreturn (ACCEL_LOG_FATAL , "Unable to create opcache_locks: %s (%d)" , strerror (errno ), errno );
118
+ }
119
+
120
+ pthread_mutexattr_t mutexattr ;
121
+ pthread_mutexattr_init (& mutexattr );
122
+ pthread_mutexattr_setpshared (& mutexattr , PTHREAD_PROCESS_SHARED );
123
+ pthread_mutex_init (& opcache_locks -> alloc , & mutexattr );
124
+ #endif // HAVE_PTHREAD_PROCESS_SHARED
125
+
91
126
#if defined(__linux__ ) && defined(HAVE_MEMFD_CREATE )
92
127
/* on Linux, we can use a memfd instead of a "real" file, so
93
128
* we can do this without a writable filesystem and without
@@ -108,7 +143,7 @@ void zend_shared_alloc_create_lock(char *lockfile_path)
108
143
109
144
fchmod (lock_file , 0666 );
110
145
111
- val = fcntl (lock_file , F_GETFD , 0 );
146
+ int val = fcntl (lock_file , F_GETFD , 0 );
112
147
val |= FD_CLOEXEC ;
113
148
fcntl (lock_file , F_SETFD , val );
114
149
@@ -320,6 +355,12 @@ void zend_shared_alloc_shutdown(void)
320
355
}
321
356
ZSMMG (shared_segments ) = NULL ;
322
357
g_shared_alloc_handler = NULL ;
358
+
359
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
360
+ pthread_mutex_destroy (& opcache_locks -> alloc );
361
+ munmap (opcache_locks , sizeof (opcache_locks ));
362
+ #endif
363
+
323
364
#ifndef ZEND_WIN32
324
365
close (lock_file );
325
366
@@ -471,18 +512,20 @@ void zend_shared_alloc_lock(void)
471
512
{
472
513
ZEND_ASSERT (!ZCG (locked ));
473
514
474
- #ifndef ZEND_WIN32
515
+ #if !defined(ZEND_WIN32 ) && defined(ZTS )
516
+ tsrm_mutex_lock (zts_lock );
517
+ #endif
518
+
519
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
520
+ pthread_mutex_lock (& opcache_locks -> alloc );
521
+ #elif !defined(ZEND_WIN32 )
475
522
struct flock mem_write_lock ;
476
523
477
524
mem_write_lock .l_type = F_WRLCK ;
478
525
mem_write_lock .l_whence = SEEK_SET ;
479
526
mem_write_lock .l_start = 0 ;
480
527
mem_write_lock .l_len = 1 ;
481
528
482
- #ifdef ZTS
483
- tsrm_mutex_lock (zts_lock );
484
- #endif
485
-
486
529
#if 0
487
530
/* this will happen once per process, and will un-globalize mem_write_lock */
488
531
if (mem_write_lock .l_pid == -1 ) {
@@ -510,7 +553,7 @@ void zend_shared_alloc_unlock(void)
510
553
{
511
554
ZEND_ASSERT (ZCG (locked ));
512
555
513
- #ifndef ZEND_WIN32
556
+ #if !defined( ZEND_WIN32 ) && !defined( HAVE_PTHREAD_PROCESS_SHARED )
514
557
struct flock mem_write_unlock ;
515
558
516
559
mem_write_unlock .l_type = F_UNLCK ;
@@ -521,16 +564,19 @@ void zend_shared_alloc_unlock(void)
521
564
522
565
ZCG (locked ) = 0 ;
523
566
524
- #ifndef ZEND_WIN32
567
+ #ifdef HAVE_PTHREAD_PROCESS_SHARED
568
+ pthread_mutex_unlock (& opcache_locks -> alloc );
569
+ #elif !defined(ZEND_WIN32 )
525
570
if (fcntl (lock_file , F_SETLK , & mem_write_unlock ) == -1 ) {
526
571
zend_accel_error_noreturn (ACCEL_LOG_ERROR , "Cannot remove lock - %s (%d)" , strerror (errno ), errno );
527
572
}
528
- #ifdef ZTS
529
- tsrm_mutex_unlock (zts_lock );
530
- #endif
531
573
#else
532
574
zend_shared_alloc_unlock_win32 ();
533
575
#endif
576
+
577
+ #if !defined(ZEND_WIN32 ) && defined(ZTS )
578
+ tsrm_mutex_unlock (zts_lock );
579
+ #endif
534
580
}
535
581
536
582
void zend_shared_alloc_init_xlat_table (void )
0 commit comments