]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Update private threading API.
authorMichael Sweet <michael.r.sweet@gmail.com>
Mon, 30 May 2016 23:45:17 +0000 (19:45 -0400)
committerMichael Sweet <michael.r.sweet@gmail.com>
Mon, 30 May 2016 23:45:17 +0000 (19:45 -0400)
cups/thread-private.h
cups/thread.c

index 9acae0ec7f86a2ef09514c19777d186dcd77af3c..64897a350ce3ed26239f4ce191ed38d8adc78d94 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Private threading definitions for CUPS.
  *
- * Copyright 2009-2014 by Apple Inc.
+ * Copyright 2009-2016 by Apple Inc.
  *
  * These coded instructions, statements, and computer programs are the
  * property of Apple Inc. and are protected by Federal copyright
@@ -31,22 +31,27 @@ extern "C" {
 #  endif /* __cplusplus */
 
 
-#  ifdef HAVE_PTHREAD_H
+#  ifdef HAVE_PTHREAD_H                        /* POSIX threading */
 #    include <pthread.h>
 typedef void *(*_cups_thread_func_t)(void *arg);
+typedef pthread_t _cups_thread_t;
+typedef pthread_cond_t _cups_cond_t;
 typedef pthread_mutex_t _cups_mutex_t;
 typedef pthread_rwlock_t _cups_rwlock_t;
 typedef pthread_key_t  _cups_threadkey_t;
+#    define _CUPS_COND_INITIALIZER PTHREAD_COND_INITIALIZER
 #    define _CUPS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
 #    define _CUPS_RWLOCK_INITIALIZER PTHREAD_RWLOCK_INITIALIZER
 #    define _CUPS_THREADKEY_INITIALIZER 0
 #    define _cupsThreadGetData(k) pthread_getspecific(k)
 #    define _cupsThreadSetData(k,p) pthread_setspecific(k,p)
 
-#  elif defined(WIN32)
+#  elif defined(WIN32)                 /* Windows threading */
 #    include <winsock2.h>
 #    include <windows.h>
 typedef void *(__stdcall *_cups_thread_func_t)(void *arg);
+typedef int _cups_thread_t;
+typedef char _cups_cond_t;             /* TODO: Implement Win32 conditional */
 typedef struct _cups_mutex_s
 {
   int                  m_init;         /* Flag for on-demand initialization */
@@ -55,17 +60,21 @@ typedef struct _cups_mutex_s
 } _cups_mutex_t;
 typedef _cups_mutex_t _cups_rwlock_t;  /* TODO: Implement Win32 reader/writer lock */
 typedef DWORD  _cups_threadkey_t;
+#    define _CUPS_COND_INITIALIZER 0
 #    define _CUPS_MUTEX_INITIALIZER { 0, 0 }
 #    define _CUPS_RWLOCK_INITIALIZER { 0, 0 }
 #    define _CUPS_THREADKEY_INITIALIZER 0
 #    define _cupsThreadGetData(k) TlsGetValue(k)
 #    define _cupsThreadSetData(k,p) TlsSetValue(k,p)
 
-#  else
+#  else                                        /* No threading */
 typedef void   *(*_cups_thread_func_t)(void *arg);
+typedef int    _cups_thread_t;
+typedef char   _cups_cond_t;
 typedef char   _cups_mutex_t;
 typedef char   _cups_rwlock_t;
 typedef void   *_cups_threadkey_t;
+#    define _CUPS_COND_INITIALIZER 0
 #    define _CUPS_MUTEX_INITIALIZER 0
 #    define _CUPS_RWLOCK_INITIALIZER 0
 #    define _CUPS_THREADKEY_INITIALIZER (void *)0
@@ -78,6 +87,9 @@ typedef void  *_cups_threadkey_t;
  * Functions...
  */
 
+extern void    _cupsCondBroadcast(_cups_cond_t *cond);
+extern void    _cupsCondInit(_cups_cond_t *cond);
+extern void    _cupsCondWait(_cups_cond_t *cond, _cups_mutex_t *mutex, double timeout);
 extern void    _cupsMutexInit(_cups_mutex_t *mutex);
 extern void    _cupsMutexLock(_cups_mutex_t *mutex);
 extern void    _cupsMutexUnlock(_cups_mutex_t *mutex);
@@ -85,8 +97,9 @@ extern void   _cupsRWInit(_cups_rwlock_t *rwlock);
 extern void    _cupsRWLockRead(_cups_rwlock_t *rwlock);
 extern void    _cupsRWLockWrite(_cups_rwlock_t *rwlock);
 extern void    _cupsRWUnlock(_cups_rwlock_t *rwlock);
-extern int     _cupsThreadCreate(_cups_thread_func_t func, void *arg);
-
+extern void    _cupsThreadCancel(_cups_thread_t thread);
+extern _cups_thread_t _cupsThreadCreate(_cups_thread_func_t func, void *arg);
+extern void    *_cupsThreadWait(_cups_thread_t thread);
 
 #  ifdef __cplusplus
 }
index 558ac9aaf9f0e1fe8ddbaed15583751fb3a9db2e..bd947b4ecdb0103e254f9f70a1700f209caa1e63 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Threading primitives for CUPS.
  *
- * Copyright 2009-2014 by Apple Inc.
+ * Copyright 2009-2016 by Apple Inc.
  *
  * These coded instructions, statements, and computer programs are the
  * property of Apple Inc. and are protected by Federal copyright
 
 
 #if defined(HAVE_PTHREAD_H)
+/*
+ * '_cupsCondBroadcast()' - Wake up waiting threads.
+ */
+
+void
+_cupsCondBroadcast(_cups_cond_t *cond) /* I - Condition */
+{
+  pthread_cond_broadcast(cond);
+}
+
+
+/*
+ * '_cupsCondInit()' - Initialize a condition variable.
+ */
+
+void
+_cupsCondInit(_cups_cond_t *cond)      /* I - Condition */
+{
+  pthread_cond_init(cond, NULL);
+}
+
+
+/*
+ * '_cupsCondWait()' - Wait for a condition with optional timeout.
+ */
+
+void
+_cupsCondWait(_cups_cond_t  *cond,     /* I - Condition */
+              _cups_mutex_t *mutex,    /* I - Mutex */
+             double        timeout)    /* I - Timeout in seconds (0 or negative for none) */
+{
+  if (timeout > 0.0)
+  {
+    struct timespec abstime;           /* Timeout */
+
+    abstime.tv_sec  = (long)timeout;
+    abstime.tv_nsec = (long)(1000000000 * (timeout - (long)timeout));
+
+    pthread_cond_timedwait(cond, mutex, &abstime);
+  }
+  else
+    pthread_cond_wait(cond, mutex);
+}
+
+
 /*
  * '_cupsMutexInit()' - Initialize a mutex.
  */
@@ -98,18 +143,49 @@ _cupsRWUnlock(_cups_rwlock_t *rwlock)      /* I - Reader/writer lock */
 }
 
 
+/*
+ * '_cupsThreadCancel()' - Cancel (kill) a thread.
+ */
+
+void
+_cupsThreadCancel(_cups_thread_t thread)/* I - Thread ID */
+{
+  pthread_cancel(thread);
+}
+
+
 /*
  * '_cupsThreadCreate()' - Create a thread.
  */
 
-int                                    /* O - 0 on failure, 1 on success */
+_cups_thread_t                         /* O - Thread ID */
 _cupsThreadCreate(
     _cups_thread_func_t func,          /* I - Entry point */
     void                *arg)          /* I - Entry point context */
 {
   pthread_t thread;
 
-  return (pthread_create(&thread, NULL, (void *(*)(void *))func, arg) == 0);
+  if (pthread_create(&thread, NULL, (void *(*)(void *))func, arg))
+    return (0);
+  else
+    return (thread);
+}
+
+
+/*
+ * '_cupsThreadWait()' - Wait for a thread to exit.
+ */
+
+void *                                 /* O - Return value */
+_cupsThreadWait(_cups_thread_t thread) /* I - Thread ID */
+{
+  void *ret;                           /* Return value */
+
+
+  if (pthread_join(thread, &ret))
+    return (NULL);
+  else
+    return (ret);
 }
 
 
@@ -208,17 +284,38 @@ _cupsRWUnlock(_cups_rwlock_t *rwlock)     /* I - Reader/writer lock */
 }
 
 
+/*
+ * '_cupsThreadCancel()' - Cancel (kill) a thread.
+ */
+
+void
+_cupsThreadCancel(_cups_thread_t thread)/* I - Thread ID */
+{
+  // TODO: Implement me
+}
+
+
 /*
  * '_cupsThreadCreate()' - Create a thread.
  */
 
-int                                    /* O - 0 on failure, 1 on success */
+_cups_thread_t                         /* O - Thread ID */
 _cupsThreadCreate(
     _cups_thread_func_t func,          /* I - Entry point */
     void                *arg)          /* I - Entry point context */
 {
-  return (_beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL)
-             != 0);
+  return (_beginthreadex(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL));
+}
+
+
+/*
+ * '_cupsThreadWait()' - Wait for a thread to exit.
+ */
+
+void *                                 /* O - Return value */
+_cupsThreadWait(_cups_thread_t thread) /* I - Thread ID */
+{
+  // TODO: Implement me
 }
 
 
@@ -300,11 +397,22 @@ _cupsRWUnlock(_cups_rwlock_t *rwlock)     /* I - Reader/writer lock */
 }
 
 
+/*
+ * '_cupsThreadCancel()' - Cancel (kill) a thread.
+ */
+
+void
+_cupsThreadCancel(_cups_thread_t thread)/* I - Thread ID */
+{
+  (void)thread;
+}
+
+
 /*
  * '_cupsThreadCreate()' - Create a thread.
  */
 
-int                                    /* O - 0 on failure, 1 on success */
+_cups_thread_t                         /* O - Thread ID */
 _cupsThreadCreate(
     _cups_thread_func_t func,          /* I - Entry point */
     void                *arg)          /* I - Entry point context */
@@ -317,4 +425,18 @@ _cupsThreadCreate(
 
   return (0);
 }
+
+
+/*
+ * '_cupsThreadWait()' - Wait for a thread to exit.
+ */
+
+void *                                 /* O - Return value */
+_cupsThreadWait(_cups_thread_t thread) /* I - Thread ID */
+{
+  (void)thread;
+
+  return (NULL);
+}
+
 #endif /* HAVE_PTHREAD_H */