Increase to 100. 16 was rather low. (g_private_new_win32_impl): Can't use
[platform/upstream/glib.git] / gthread / gthread-posix.c
index fd3d758..0ead204 100644 (file)
@@ -31,6 +31,8 @@
  * MT safe
  */
 
+#include <config.h>
+
 #include <pthread.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <sched.h>
 #endif
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-# include <sys/resource.h>
-# define PID_IN_THREAD(thread) (*(pid_t*)(((gchar*)thread)+sizeof(pthread_t)))
-# define SET_PRIO(pid, prio)                                           \
-  posix_check_cmd_prio ((setpriority (PRIO_PROCESS, (pid),             \
-                              g_thread_priority_map [prio]) == -1) ?   \
-                                 (errno == EACCES ? EPERM : errno ): 0)
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
 #define posix_check_err(err, name) G_STMT_START{                       \
   int error = (err);                                                   \
   if (error)                                                           \
@@ -104,29 +97,53 @@ static gboolean posix_check_cmd_prio_warned = FALSE;
 # error This should not happen. Contact the GLib team.
 #endif
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-# define PRIORITY_LOW_VALUE 15
-# define PRIORITY_NORMAL_VALUE 0
-# define PRIORITY_HIGH_VALUE -15
-# define PRIORITY_URGENT_VALUE -20
-#elif defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
+#if defined (POSIX_MIN_PRIORITY) && defined (POSIX_MAX_PRIORITY)
 # define HAVE_PRIORITIES 1
-# define PRIORITY_LOW_VALUE POSIX_MIN_PRIORITY
-# define PRIORITY_URGENT_VALUE POSIX_MAX_PRIORITY
+static gint priority_normal_value;
+# ifdef __FreeBSD__
+   /* FreeBSD threads use different priority values from the POSIX_
+    * defines so we just set them here. The corresponding macros
+    * PTHREAD_MIN_PRIORITY and PTHREAD_MAX_PRIORITY are implied to be
+    * exported by the docs, but they aren't.
+    */
+#  define PRIORITY_LOW_VALUE      0
+#  define PRIORITY_URGENT_VALUE   31
+# else /* !__FreeBSD__ */
+#  define PRIORITY_LOW_VALUE      POSIX_MIN_PRIORITY
+#  define PRIORITY_URGENT_VALUE   POSIX_MAX_PRIORITY
+# endif /* !__FreeBSD__ */
+# define PRIORITY_NORMAL_VALUE    priority_normal_value
 #endif /* POSIX_MIN_PRIORITY && POSIX_MAX_PRIORITY */
 
 static gulong g_thread_min_stack_size = 0;
 
 #define G_MUTEX_SIZE (sizeof (pthread_mutex_t))
 
-#ifdef _SC_THREAD_STACK_MIN
+#if defined(_SC_THREAD_STACK_MIN) || defined (HAVE_PRIORITIES)
 #define HAVE_G_THREAD_IMPL_INIT
 static void 
-g_thread_impl_init()
+g_thread_impl_init(void)
 {
+#ifdef _SC_THREAD_STACK_MIN
   g_thread_min_stack_size = MAX (sysconf (_SC_THREAD_STACK_MIN), 0);
-}
 #endif /* _SC_THREAD_STACK_MIN */
+#ifdef HAVE_PRIORITIES
+# ifdef G_THREADS_IMPL_POSIX
+  {
+    struct sched_param sched;
+    int policy;
+    posix_check_cmd (pthread_getschedparam (pthread_self(), &policy, &sched));
+    priority_normal_value = sched.sched_priority;
+  }
+# else /* G_THREADS_IMPL_DCE */
+  posix_check_cmd (priority_normal_value = 
+                  pthread_getprio (*(pthread_t*)thread, 
+                                   g_thread_priority_map [priority]));
+# endif
+#endif /* HAVE_PRIORITIES */
+
+}
+#endif /* _SC_THREAD_STACK_MIN || HAVE_PRIORITIES */
 
 static GMutex *
 g_mutex_new_posix_impl (void)
@@ -206,7 +223,9 @@ g_cond_timed_wait_posix_impl (GCond * cond,
 
   end_time.tv_sec = abs_time->tv_sec;
   end_time.tv_nsec = abs_time->tv_usec * (G_NSEC_PER_SEC / G_USEC_PER_SEC);
-  g_assert (end_time.tv_nsec < G_NSEC_PER_SEC);
+
+  g_return_val_if_fail (end_time.tv_nsec < G_NSEC_PER_SEC, TRUE);
+
   result = pthread_cond_timedwait ((pthread_cond_t *) cond,
                                   (pthread_mutex_t *) entered_mutex,
                                   &end_time);
@@ -214,7 +233,7 @@ g_cond_timed_wait_posix_impl (GCond * cond,
 #ifdef G_THREADS_IMPL_POSIX
   timed_out = (result == ETIMEDOUT);
 #else /* G_THREADS_IMPL_DCE */
-  timed_out = (result == -1) && (errno = EAGAIN);
+  timed_out = (result == -1) && (errno == EAGAIN);
 #endif
 
   if (!timed_out)
@@ -265,27 +284,6 @@ g_private_get_posix_impl (GPrivate * private_key)
 #endif
 }
 
-#ifdef G_THREAD_USE_PID_SURROGATE
-struct proxy_data
-{
-  GThreadFunc thread_func;
-  gpointer arg;
-  gpointer thread;
-  GThreadPriority priority;
-};
-
-static void
-g_thread_create_posix_impl_proxy (struct proxy_data *data)
-{
-  GThreadFunc thread_func = data->thread_func;
-  GThreadFunc arg = data->arg;
-  PID_IN_THREAD (data->thread) = getpid();
-  SET_PRIO (PID_IN_THREAD (data->thread), data->priority);
-  g_free (data);
-  thread_func (arg);
-}
-#endif /* G_THREAD_USE_PID_SURROGATE */
-
 static void
 g_thread_create_posix_impl (GThreadFunc thread_func, 
                            gpointer arg, 
@@ -309,7 +307,9 @@ g_thread_create_posix_impl (GThreadFunc thread_func,
   if (stack_size)
     {
       stack_size = MAX (g_thread_min_stack_size, stack_size);
-      posix_check_cmd (pthread_attr_setstacksize (&attr, stack_size));
+      /* No error check here, because some systems can't do it and
+       * we simply don't want threads to fail because of that. */
+      pthread_attr_setstacksize (&attr, stack_size);
     }
 #endif /* HAVE_PTHREAD_ATTR_SETSTACKSIZE */
 
@@ -325,35 +325,21 @@ g_thread_create_posix_impl (GThreadFunc thread_func,
           joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED));
 #endif /* G_THREADS_IMPL_POSIX */
   
-#ifdef G_THREAD_USE_PID_SURROGATE
-  {
-    struct proxy_data *data = g_new (struct proxy_data, 1); 
-    data->thread_func = thread_func;
-    data->arg = arg;
-    data->thread = thread;
-    data->priority = priority;
-    PID_IN_THREAD (thread) = 0;
-    ret = posix_error (pthread_create (thread, &attr, (void* (*)(void*))
-                                      g_thread_create_posix_impl_proxy, 
-                                      data));
-  }
-#else /* G_THREAD_USE_PID_SURROGATE */
-# ifdef HAVE_PRIORITIES
-#  ifdef G_THREADS_IMPL_POSIX
+#ifdef HAVE_PRIORITIES
+# ifdef G_THREADS_IMPL_POSIX
   {
     struct sched_param sched;
     posix_check_cmd (pthread_attr_getschedparam (&attr, &sched));
     sched.sched_priority = g_thread_priority_map [priority];
     posix_check_cmd_prio (pthread_attr_setschedparam (&attr, &sched));
   }
-#  else /* G_THREADS_IMPL_DCE */
+# else /* G_THREADS_IMPL_DCE */
   posix_check_cmd_prio 
     (pthread_attr_setprio (&attr, g_thread_priority_map [priority]));
-#  endif /* G_THREADS_IMPL_DCE */
-# endif /* HAVE_PRIORITIES */
+# endif /* G_THREADS_IMPL_DCE */
+#endif /* HAVE_PRIORITIES */
   ret = posix_error (pthread_create (thread, &attr, 
                                     (void* (*)(void*))thread_func, arg));
-#endif /* !G_THREAD_USE_PID_SURROGATE */
 
   posix_check_cmd (pthread_attr_destroy (&attr));
 
@@ -411,21 +397,19 @@ g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
   posix_check_cmd_prio (pthread_setprio (*(pthread_t*)thread, 
                                         g_thread_priority_map [priority]));
 # endif
-#elif defined (G_THREAD_USE_PID_SURROGATE)
-  /* If the addressed thread hasn't yet been able to provide it's pid,
-   * we ignore the request. Should be more than rare */
-  if (PID_IN_THREAD (thread) != 0)
-    SET_PRIO (PID_IN_THREAD (thread), priority);
-#endif /* G_THREAD_USE_PID_SURROGATE */
+#endif /* HAVE_PRIORITIES */
 }
 
 static void
 g_thread_self_posix_impl (gpointer thread)
 {
   *(pthread_t*)thread = pthread_self();
-#ifdef G_THREAD_USE_PID_SURROGATE
-  PID_IN_THREAD (thread) = getpid();
-#endif /* G_THREAD_USE_PID_SURROGATE */
+}
+
+static gboolean
+g_thread_equal_posix_impl (gpointer thread1, gpointer thread2)
+{
+  return (pthread_equal (*(pthread_t*)thread1, *(pthread_t*)thread2) != 0);
 }
 
 static GThreadFunctions g_thread_functions_for_glib_use_default =
@@ -449,5 +433,6 @@ static GThreadFunctions g_thread_functions_for_glib_use_default =
   g_thread_join_posix_impl,
   g_thread_exit_posix_impl,
   g_thread_set_priority_posix_impl,
-  g_thread_self_posix_impl
+  g_thread_self_posix_impl,
+  g_thread_equal_posix_impl
 };