util/sha1: rework _mesa_sha1_{init,final}
[mesa.git] / src / util / u_thread.h
1 /**************************************************************************
2 *
3 * Copyright 1999-2006 Brian Paul
4 * Copyright 2008 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 *
25 **************************************************************************/
26
27 #ifndef U_THREAD_H_
28 #define U_THREAD_H_
29
30 #include <stdint.h>
31
32 #include "c11/threads.h"
33
34 #ifdef HAVE_PTHREAD
35 #include <signal.h>
36 #endif
37
38
39 static inline thrd_t u_thread_create(int (*routine)(void *), void *param)
40 {
41 thrd_t thread;
42 #ifdef HAVE_PTHREAD
43 sigset_t saved_set, new_set;
44 int ret;
45
46 sigfillset(&new_set);
47 pthread_sigmask(SIG_SETMASK, &new_set, &saved_set);
48 ret = thrd_create( &thread, routine, param );
49 pthread_sigmask(SIG_SETMASK, &saved_set, NULL);
50 #else
51 int ret;
52 ret = thrd_create( &thread, routine, param );
53 #endif
54 if (ret)
55 return 0;
56
57 return thread;
58 }
59
60 static inline void u_thread_setname( const char *name )
61 {
62 #if defined(HAVE_PTHREAD)
63 # if defined(__GNU_LIBRARY__) && defined(__GLIBC__) && defined(__GLIBC_MINOR__) && \
64 (__GLIBC__ >= 3 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 12))
65 pthread_setname_np(pthread_self(), name);
66 # endif
67 #endif
68 (void)name;
69 }
70
71 /*
72 * Thread statistics.
73 */
74
75 /* Return the time of a thread's CPU time clock. */
76 static inline int64_t
77 u_thread_get_time_nano(thrd_t thread)
78 {
79 #if defined(__linux__) && defined(HAVE_PTHREAD)
80 struct timespec ts;
81 clockid_t cid;
82
83 pthread_getcpuclockid(thread, &cid);
84 clock_gettime(cid, &ts);
85 return (int64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
86 #else
87 return 0;
88 #endif
89 }
90
91 #endif /* U_THREAD_H_ */