aboutsummaryrefslogtreecommitdiff
path: root/src/os/unix
diff options
context:
space:
mode:
Diffstat (limited to 'src/os/unix')
-rw-r--r--src/os/unix/ngx_linux_config.h2
-rw-r--r--src/os/unix/ngx_thread.h52
-rw-r--r--src/os/unix/ngx_thread_cond.c87
-rw-r--r--src/os/unix/ngx_thread_id.c70
-rw-r--r--src/os/unix/ngx_thread_mutex.c174
5 files changed, 384 insertions, 1 deletions
diff --git a/src/os/unix/ngx_linux_config.h b/src/os/unix/ngx_linux_config.h
index c6c02c93e..0c0b168d7 100644
--- a/src/os/unix/ngx_linux_config.h
+++ b/src/os/unix/ngx_linux_config.h
@@ -93,11 +93,11 @@ extern ssize_t sendfile(int s, int fd, int32_t *offset, size_t size);
#endif
-#if (NGX_HAVE_FILE_AIO)
#if (NGX_HAVE_SYS_EVENTFD_H)
#include <sys/eventfd.h>
#endif
#include <sys/syscall.h>
+#if (NGX_HAVE_FILE_AIO)
#include <linux/aio_abi.h>
typedef struct iocb ngx_aiocb_t;
#endif
diff --git a/src/os/unix/ngx_thread.h b/src/os/unix/ngx_thread.h
index 2077b3df6..9d5f72491 100644
--- a/src/os/unix/ngx_thread.h
+++ b/src/os/unix/ngx_thread.h
@@ -111,9 +111,61 @@ ngx_int_t ngx_cond_signal(ngx_cond_t *cv);
#define ngx_thread_volatile
+#if (NGX_THREADS)
+
+#include <pthread.h>
+
+
+typedef pthread_mutex_t ngx_thread_mutex_t;
+
+ngx_int_t ngx_thread_mutex_create(ngx_thread_mutex_t *mtx, ngx_log_t *log);
+ngx_int_t ngx_thread_mutex_destroy(ngx_thread_mutex_t *mtx, ngx_log_t *log);
+ngx_int_t ngx_thread_mutex_lock(ngx_thread_mutex_t *mtx, ngx_log_t *log);
+ngx_int_t ngx_thread_mutex_unlock(ngx_thread_mutex_t *mtx, ngx_log_t *log);
+
+
+typedef pthread_cond_t ngx_thread_cond_t;
+
+ngx_int_t ngx_thread_cond_create(ngx_thread_cond_t *cond, ngx_log_t *log);
+ngx_int_t ngx_thread_cond_destroy(ngx_thread_cond_t *cond, ngx_log_t *log);
+ngx_int_t ngx_thread_cond_signal(ngx_thread_cond_t *cond, ngx_log_t *log);
+ngx_int_t ngx_thread_cond_wait(ngx_thread_cond_t *cond, ngx_thread_mutex_t *mtx,
+ ngx_log_t *log);
+
+
+#if (NGX_LINUX)
+
+typedef pid_t ngx_tid_t;
+#define NGX_TID_T_FMT "%P"
+
+#elif (NGX_FREEBSD)
+
+typedef uint32_t ngx_tid_t;
+#define NGX_TID_T_FMT "%uD"
+
+#elif (NGX_DARWIN)
+
+typedef uint64_t ngx_tid_t;
+#define NGX_TID_T_FMT "%uA"
+
+#else
+
+typedef uint64_t ngx_tid_t;
+#define NGX_TID_T_FMT "%uA"
+
+#endif
+
+ngx_tid_t ngx_thread_tid(void);
+
+#define ngx_log_tid ngx_thread_tid()
+
+#else
+
#define ngx_log_tid 0
#define NGX_TID_T_FMT "%d"
+#endif
+
#define ngx_mutex_trylock(m) NGX_OK
#define ngx_mutex_lock(m)
#define ngx_mutex_unlock(m)
diff --git a/src/os/unix/ngx_thread_cond.c b/src/os/unix/ngx_thread_cond.c
new file mode 100644
index 000000000..f5246966a
--- /dev/null
+++ b/src/os/unix/ngx_thread_cond.c
@@ -0,0 +1,87 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+ngx_int_t
+ngx_thread_cond_create(ngx_thread_cond_t *cond, ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ err = pthread_cond_init(cond, NULL);
+ if (err == 0) {
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_cond_init(%p)", cond);
+ return NGX_OK;
+ }
+
+ ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_init() failed");
+ return NGX_ERROR;
+}
+
+
+ngx_int_t
+ngx_thread_cond_destroy(ngx_thread_cond_t *cond, ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ err = pthread_cond_destroy(cond);
+ if (err == 0) {
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_cond_destroy(%p)", cond);
+ return NGX_OK;
+ }
+
+ ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_destroy() failed");
+ return NGX_ERROR;
+}
+
+
+ngx_int_t
+ngx_thread_cond_signal(ngx_thread_cond_t *cond, ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ err = pthread_cond_signal(cond);
+ if (err == 0) {
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_cond_signal(%p)", cond);
+ return NGX_OK;
+ }
+
+ ngx_log_error(NGX_LOG_EMERG, log, err, "pthread_cond_signal() failed");
+ return NGX_ERROR;
+}
+
+
+ngx_int_t
+ngx_thread_cond_wait(ngx_thread_cond_t *cond, ngx_thread_mutex_t *mtx,
+ ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_cond_wait(%p) enter", cond);
+
+ err = pthread_cond_wait(cond, mtx);
+
+#if 0
+ ngx_time_update();
+#endif
+
+ if (err == 0) {
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_cond_wait(%p) exit", cond);
+ return NGX_OK;
+ }
+
+ ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_cond_wait() failed");
+
+ return NGX_ERROR;
+}
diff --git a/src/os/unix/ngx_thread_id.c b/src/os/unix/ngx_thread_id.c
new file mode 100644
index 000000000..5174f1abc
--- /dev/null
+++ b/src/os/unix/ngx_thread_id.c
@@ -0,0 +1,70 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Nginx, Inc.
+ */
+
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+#include <ngx_thread_pool.h>
+
+
+#if (NGX_LINUX)
+
+/*
+ * Linux thread id is a pid of thread created by clone(2),
+ * glibc does not provide a wrapper for gettid().
+ */
+
+ngx_tid_t
+ngx_thread_tid(void)
+{
+ return syscall(SYS_gettid);
+}
+
+#elif (NGX_FREEBSD) && (__FreeBSD_version >= 900031)
+
+#include <pthread_np.h>
+
+ngx_tid_t
+ngx_thread_tid(void)
+{
+ return pthread_getthreadid_np();
+}
+
+#elif (NGX_DARWIN)
+
+/*
+ * MacOSX thread has two thread ids:
+ *
+ * 1) MacOSX 10.6 (Snow Leoprad) has pthread_threadid_np() returning
+ * an uint64_t value, which is obtained using the __thread_selfid()
+ * syscall. It is a number above 300,000.
+ */
+
+ngx_tid_t
+ngx_thread_tid(void)
+{
+ uint64_t tid;
+
+ (void) pthread_threadid_np(NULL, &tid);
+ return tid;
+}
+
+/*
+ * 2) Kernel thread mach_port_t returned by pthread_mach_thread_np().
+ * It is a number in range 100-100,000.
+ *
+ * return pthread_mach_thread_np(pthread_self());
+ */
+
+#else
+
+ngx_tid_t
+ngx_thread_tid(void)
+{
+ return (uint64_t) (uintptr_t) pthread_self();
+}
+
+#endif
diff --git a/src/os/unix/ngx_thread_mutex.c b/src/os/unix/ngx_thread_mutex.c
new file mode 100644
index 000000000..6e8385ef5
--- /dev/null
+++ b/src/os/unix/ngx_thread_mutex.c
@@ -0,0 +1,174 @@
+
+/*
+ * Copyright (C) Igor Sysoev
+ * Copyright (C) Nginx, Inc.
+ */
+
+#include <ngx_config.h>
+#include <ngx_core.h>
+
+
+/*
+ * All modern pthread mutex implementations try to acquire a lock
+ * atomically in userland before going to sleep in kernel. Some
+ * spins before the sleeping.
+ *
+ * In Solaris since version 8 all mutex types spin before sleeping.
+ * The default spin count is 1000. It can be overridden using
+ * _THREAD_ADAPTIVE_SPIN=100 environment variable.
+ *
+ * In MacOSX all mutex types spin to acquire a lock protecting a mutex's
+ * internals. If the mutex is busy, thread calls Mach semaphore_wait().
+ *
+ *
+ * PTHREAD_MUTEX_NORMAL lacks deadlock detection and is the fastest
+ * mutex type.
+ *
+ * Linux: No spinning. The internal name PTHREAD_MUTEX_TIMED_NP
+ * remains from the times when pthread_mutex_timedlock() was
+ * non-standard extension. Alias name: PTHREAD_MUTEX_FAST_NP.
+ * FreeBSD: No spinning.
+ *
+ *
+ * PTHREAD_MUTEX_ERRORCHECK is usually as fast as PTHREAD_MUTEX_NORMAL
+ * yet has lightweight deadlock detection.
+ *
+ * Linux: No spinning. The internal name: PTHREAD_MUTEX_ERRORCHECK_NP.
+ * FreeBSD: No spinning.
+ *
+ *
+ * PTHREAD_MUTEX_RECURSIVE allows recursive locking.
+ *
+ * Linux: No spinning. The internal name: PTHREAD_MUTEX_RECURSIVE_NP.
+ * FreeBSD: No spinning.
+ *
+ *
+ * PTHREAD_MUTEX_ADAPTIVE_NP spins on SMP systems before sleeping.
+ *
+ * Linux: No deadlock detection. Dynamically changes a spin count
+ * for each mutex from 10 to 100 based on spin count taken
+ * previously.
+ * FreeBSD: Deadlock detection. The default spin count is 2000.
+ * It can be overriden using LIBPTHREAD_SPINLOOPS environment
+ * variable or by pthread_mutex_setspinloops_np(). If a lock
+ * is still busy, sched_yield() can be called on both UP and
+ * SMP systems. The default yield loop count is zero, but
+ * it can be set by LIBPTHREAD_YIELDLOOPS environment
+ * variable or by pthread_mutex_setyieldloops_np().
+ * Solaris: No PTHREAD_MUTEX_ADAPTIVE_NP.
+ * MacOSX: No PTHREAD_MUTEX_ADAPTIVE_NP.
+ *
+ *
+ * PTHREAD_MUTEX_ELISION_NP is a Linux extension to elide locks using
+ * Intel Restricted Transactional Memory. It is the most suitable for
+ * rwlock pattern access because it allows simultaneous reads without lock.
+ * Supported since glibc 2.18.
+ *
+ *
+ * PTHREAD_MUTEX_DEFAULT is default mutex type.
+ *
+ * Linux: PTHREAD_MUTEX_NORMAL.
+ * FreeBSD: PTHREAD_MUTEX_ERRORCHECK.
+ * Solaris: PTHREAD_MUTEX_NORMAL.
+ * MacOSX: PTHREAD_MUTEX_NORMAL.
+ */
+
+
+ngx_int_t
+ngx_thread_mutex_create(ngx_thread_mutex_t *mtx, ngx_log_t *log)
+{
+ ngx_err_t err;
+ pthread_mutexattr_t attr;
+
+ err = pthread_mutexattr_init(&attr);
+ if (err != 0) {
+ ngx_log_error(NGX_LOG_EMERG, log, err,
+ "pthread_mutexattr_init() failed");
+ return NGX_ERROR;
+ }
+
+ err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
+ if (err != 0) {
+ ngx_log_error(NGX_LOG_EMERG, log, err,
+ "pthread_mutexattr_settype"
+ "(PTHREAD_MUTEX_ERRORCHECK) failed");
+ return NGX_ERROR;
+ }
+
+ err = pthread_mutex_init(mtx, &attr);
+ if (err != 0) {
+ ngx_log_error(NGX_LOG_EMERG, log, err,
+ "pthread_mutex_init() failed");
+ return NGX_ERROR;
+ }
+
+ err = pthread_mutexattr_destroy(&attr);
+ if (err != 0) {
+ ngx_log_error(NGX_LOG_ALERT, log, err,
+ "pthread_mutexattr_destroy() failed");
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_mutex_init(%p)", mtx);
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_thread_mutex_destroy(ngx_thread_mutex_t *mtx, ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ err = pthread_mutex_destroy(mtx);
+ if (err != 0) {
+ ngx_log_error(NGX_LOG_ALERT, log, err,
+ "pthread_mutex_destroy() failed");
+ return NGX_ERROR;
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_mutex_destroy(%p)", mtx);
+ return NGX_OK;
+}
+
+
+ngx_int_t
+ngx_thread_mutex_lock(ngx_thread_mutex_t *mtx, ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_mutex_lock(%p) enter", mtx);
+
+ err = pthread_mutex_lock(mtx);
+ if (err == 0) {
+ return NGX_OK;
+ }
+
+ ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_mutex_lock() failed");
+
+ return NGX_ERROR;
+}
+
+
+ngx_int_t
+ngx_thread_mutex_unlock(ngx_thread_mutex_t *mtx, ngx_log_t *log)
+{
+ ngx_err_t err;
+
+ err = pthread_mutex_unlock(mtx);
+
+#if 0
+ ngx_time_update();
+#endif
+
+ if (err == 0) {
+ ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
+ "pthread_mutex_unlock(%p) exit", mtx);
+ return NGX_OK;
+ }
+
+ ngx_log_error(NGX_LOG_ALERT, log, err, "pthread_mutex_unlock() failed");
+
+ return NGX_ERROR;
+}