annotate libcilkrts/runtime/os_mutex-unix.c @ 143:76e1cf5455ef

add cbc_gc test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 19:24:05 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* os_mutex-unix.c -*-C-*-
kono
parents:
diff changeset
2 *
kono
parents:
diff changeset
3 *************************************************************************
kono
parents:
diff changeset
4 *
kono
parents:
diff changeset
5 * Copyright (C) 2009-2016, Intel Corporation
kono
parents:
diff changeset
6 * All rights reserved.
kono
parents:
diff changeset
7 *
kono
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
kono
parents:
diff changeset
10 * are met:
kono
parents:
diff changeset
11 *
kono
parents:
diff changeset
12 * * Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
14 * * Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
16 * the documentation and/or other materials provided with the
kono
parents:
diff changeset
17 * distribution.
kono
parents:
diff changeset
18 * * Neither the name of Intel Corporation nor the names of its
kono
parents:
diff changeset
19 * contributors may be used to endorse or promote products derived
kono
parents:
diff changeset
20 * from this software without specific prior written permission.
kono
parents:
diff changeset
21 *
kono
parents:
diff changeset
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kono
parents:
diff changeset
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kono
parents:
diff changeset
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kono
parents:
diff changeset
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kono
parents:
diff changeset
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
kono
parents:
diff changeset
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
kono
parents:
diff changeset
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
kono
parents:
diff changeset
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
kono
parents:
diff changeset
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
kono
parents:
diff changeset
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
kono
parents:
diff changeset
32 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
33 * POSSIBILITY OF SUCH DAMAGE.
kono
parents:
diff changeset
34 *
kono
parents:
diff changeset
35 * *********************************************************************
kono
parents:
diff changeset
36 *
kono
parents:
diff changeset
37 * PLEASE NOTE: This file is a downstream copy of a file mainitained in
kono
parents:
diff changeset
38 * a repository at cilkplus.org. Changes made to this file that are not
kono
parents:
diff changeset
39 * submitted through the contribution process detailed at
kono
parents:
diff changeset
40 * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
kono
parents:
diff changeset
41 * time that a new version is released. Changes only submitted to the
kono
parents:
diff changeset
42 * GNU compiler collection or posted to the git repository at
kono
parents:
diff changeset
43 * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are
kono
parents:
diff changeset
44 * not tracked.
kono
parents:
diff changeset
45 *
kono
parents:
diff changeset
46 * We welcome your contributions to this open source project. Thank you
kono
parents:
diff changeset
47 * for your assistance in helping us improve Cilk Plus.
kono
parents:
diff changeset
48 **************************************************************************/
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 #include "os_mutex.h"
kono
parents:
diff changeset
51 #include "bug.h"
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 #include <stdlib.h>
kono
parents:
diff changeset
54 #include <errno.h>
kono
parents:
diff changeset
55 #include <pthread.h>
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 // contains notification macros for VTune.
kono
parents:
diff changeset
58 #include "cilk-ittnotify.h"
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 /*
kono
parents:
diff changeset
61 * OS Mutex functions.
kono
parents:
diff changeset
62 *
kono
parents:
diff changeset
63 * Not to be confused with the spinlock mutexes implemented in cilk_mutex.c
kono
parents:
diff changeset
64 */
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 struct os_mutex {
kono
parents:
diff changeset
67 pthread_mutex_t mutex; ///< On Linux, os_mutex is implemented with a pthreads mutex
kono
parents:
diff changeset
68 };
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 // Unix implementation of the global OS mutex. This will be created by the
kono
parents:
diff changeset
71 // first call to global_os_mutex_lock() and *NEVER* destroyed. On gcc-based
kono
parents:
diff changeset
72 // systems there's no way to guarantee the ordering of constructors and
kono
parents:
diff changeset
73 // destructors, so we can't be guaranteed that our destructor for a static
kono
parents:
diff changeset
74 // object will be called *after* any static destructors that may use Cilk
kono
parents:
diff changeset
75 // in the user's application
kono
parents:
diff changeset
76 static struct os_mutex *global_os_mutex = NULL;
kono
parents:
diff changeset
77
kono
parents:
diff changeset
78 /* Sometimes during shared library load malloc doesn't work.
kono
parents:
diff changeset
79 To handle that case, preallocate space for one mutex. */
kono
parents:
diff changeset
80 static struct os_mutex static_mutex;
kono
parents:
diff changeset
81 static int static_mutex_used;
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 struct os_mutex *__cilkrts_os_mutex_create(void)
kono
parents:
diff changeset
84 {
kono
parents:
diff changeset
85 int status;
kono
parents:
diff changeset
86 struct os_mutex *mutex = (struct os_mutex *)malloc(sizeof(struct os_mutex));
kono
parents:
diff changeset
87 pthread_mutexattr_t attr;
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 ITT_SYNC_CREATE(mutex, "OS Mutex");
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 if (!mutex) {
kono
parents:
diff changeset
92 if (static_mutex_used) {
kono
parents:
diff changeset
93 __cilkrts_bug("Cilk RTS library initialization failed");
kono
parents:
diff changeset
94 } else {
kono
parents:
diff changeset
95 static_mutex_used = 1;
kono
parents:
diff changeset
96 mutex = &static_mutex;
kono
parents:
diff changeset
97 }
kono
parents:
diff changeset
98 }
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 status = pthread_mutexattr_init(&attr);
kono
parents:
diff changeset
101 CILK_ASSERT (status == 0);
kono
parents:
diff changeset
102 #if defined DEBUG || CILK_LIB_DEBUG
kono
parents:
diff changeset
103 #ifdef PTHREAD_MUTEX_ERRORCHECK
kono
parents:
diff changeset
104 status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
kono
parents:
diff changeset
105 #else
kono
parents:
diff changeset
106 status = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
kono
parents:
diff changeset
107 #endif
kono
parents:
diff changeset
108 CILK_ASSERT (status == 0);
kono
parents:
diff changeset
109 #endif
kono
parents:
diff changeset
110 status = pthread_mutex_init (&mutex->mutex, &attr);
kono
parents:
diff changeset
111 CILK_ASSERT (status == 0);
kono
parents:
diff changeset
112 pthread_mutexattr_destroy(&attr);
kono
parents:
diff changeset
113
kono
parents:
diff changeset
114 return mutex;
kono
parents:
diff changeset
115 }
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 void __cilkrts_os_mutex_lock(struct os_mutex *p)
kono
parents:
diff changeset
118 {
kono
parents:
diff changeset
119 int status;
kono
parents:
diff changeset
120 status = pthread_mutex_lock (&p->mutex);
kono
parents:
diff changeset
121 ITT_SYNC_ACQUIRED(p);
kono
parents:
diff changeset
122 if (__builtin_expect(status, 0) == 0)
kono
parents:
diff changeset
123 return;
kono
parents:
diff changeset
124 if (status == EDEADLK)
kono
parents:
diff changeset
125 __cilkrts_bug("Cilk runtime error: deadlock acquiring mutex %p\n",
kono
parents:
diff changeset
126 p);
kono
parents:
diff changeset
127 else
kono
parents:
diff changeset
128 __cilkrts_bug("Cilk runtime error %d acquiring mutex %p\n",
kono
parents:
diff changeset
129 status, p);
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 int __cilkrts_os_mutex_trylock(struct os_mutex *p)
kono
parents:
diff changeset
133 {
kono
parents:
diff changeset
134 int status;
kono
parents:
diff changeset
135 status = pthread_mutex_trylock (&p->mutex);
kono
parents:
diff changeset
136 return (status == 0);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 void __cilkrts_os_mutex_unlock(struct os_mutex *p)
kono
parents:
diff changeset
140 {
kono
parents:
diff changeset
141 int status;
kono
parents:
diff changeset
142 ITT_SYNC_RELEASING(p);
kono
parents:
diff changeset
143 status = pthread_mutex_unlock (&p->mutex);
kono
parents:
diff changeset
144 CILK_ASSERT(status == 0);
kono
parents:
diff changeset
145 }
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 void __cilkrts_os_mutex_destroy(struct os_mutex *p)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 pthread_mutex_destroy (&p->mutex);
kono
parents:
diff changeset
150 if (p == &static_mutex) {
kono
parents:
diff changeset
151 static_mutex_used = 0;
kono
parents:
diff changeset
152 } else {
kono
parents:
diff changeset
153 free(p);
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 /*
kono
parents:
diff changeset
158 * create_global_os_mutex
kono
parents:
diff changeset
159 *
kono
parents:
diff changeset
160 * Function used with pthread_once to initialize the global OS mutex. Since
kono
parents:
diff changeset
161 * pthread_once requires a function which takes no parameters and has no
kono
parents:
diff changeset
162 * return value, the global OS mutex will be stored in the static (global
kono
parents:
diff changeset
163 * to the compilation unit) variable "global_os_mutex."
kono
parents:
diff changeset
164 *
kono
parents:
diff changeset
165 *
kono
parents:
diff changeset
166 * global_os_mutex will never be destroyed.
kono
parents:
diff changeset
167 */
kono
parents:
diff changeset
168 static void create_global_os_mutex(void)
kono
parents:
diff changeset
169 {
kono
parents:
diff changeset
170 CILK_ASSERT(NULL == global_os_mutex);
kono
parents:
diff changeset
171 global_os_mutex = __cilkrts_os_mutex_create();
kono
parents:
diff changeset
172 }
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 void global_os_mutex_lock(void)
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 // pthread_once_t used with pthread_once to guarantee that
kono
parents:
diff changeset
177 // create_global_os_mutex() is only called once
kono
parents:
diff changeset
178 static pthread_once_t global_os_mutex_is_initialized = PTHREAD_ONCE_INIT;
kono
parents:
diff changeset
179
kono
parents:
diff changeset
180 // Execute create_global_os_mutex once in a thread-safe manner
kono
parents:
diff changeset
181 // Note that create_global_os_mutex returns the mutex in the static
kono
parents:
diff changeset
182 // (global to the module) variable "global_os_mutex"
kono
parents:
diff changeset
183 pthread_once(&global_os_mutex_is_initialized,
kono
parents:
diff changeset
184 create_global_os_mutex);
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 // We'd better have allocated a global_os_mutex
kono
parents:
diff changeset
187 CILK_ASSERT(NULL != global_os_mutex);
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 // Acquire the global OS mutex
kono
parents:
diff changeset
190 __cilkrts_os_mutex_lock(global_os_mutex);
kono
parents:
diff changeset
191 }
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 void global_os_mutex_unlock(void)
kono
parents:
diff changeset
194 {
kono
parents:
diff changeset
195 // We'd better have allocated a global_os_mutex. This means you should
kono
parents:
diff changeset
196 // have called global_os_mutex_lock() before calling
kono
parents:
diff changeset
197 // global_os_mutex_unlock(), but this is the only check for it.
kono
parents:
diff changeset
198 CILK_ASSERT(NULL != global_os_mutex);
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 // Release the global OS mutex
kono
parents:
diff changeset
201 __cilkrts_os_mutex_unlock(global_os_mutex);
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203
kono
parents:
diff changeset
204 /* End os_mutex-unix.c */