2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libgo / runtime / thread.c
blobd43e224ffb29e06eda3104b2dcff252b091c50b0
1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 #include <errno.h>
6 #include <signal.h>
8 #include "runtime.h"
9 #include "go-assert.h"
11 /* For targets which don't have the required sync support. Really
12 these should be provided by gcc itself. FIXME. */
14 #if !defined (HAVE_SYNC_BOOL_COMPARE_AND_SWAP_4) || !defined (HAVE_SYNC_FETCH_AND_ADD_4)
16 static pthread_mutex_t sync_lock = PTHREAD_MUTEX_INITIALIZER;
18 #endif
20 #ifndef HAVE_SYNC_BOOL_COMPARE_AND_SWAP_4
22 _Bool
23 __sync_bool_compare_and_swap_4 (uint32*, uint32, uint32)
24 __attribute__ ((visibility ("hidden")));
26 _Bool
27 __sync_bool_compare_and_swap_4 (uint32* ptr, uint32 old, uint32 new)
29 int i;
30 _Bool ret;
32 i = pthread_mutex_lock (&sync_lock);
33 __go_assert (i == 0);
35 if (*ptr != old)
36 ret = 0;
37 else
39 *ptr = new;
40 ret = 1;
43 i = pthread_mutex_unlock (&sync_lock);
44 __go_assert (i == 0);
46 return ret;
49 #endif
51 #ifndef HAVE_SYNC_FETCH_AND_ADD_4
53 uint32
54 __sync_fetch_and_add_4 (uint32*, uint32)
55 __attribute__ ((visibility ("hidden")));
57 uint32
58 __sync_fetch_and_add_4 (uint32* ptr, uint32 add)
60 int i;
61 uint32 ret;
63 i = pthread_mutex_lock (&sync_lock);
64 __go_assert (i == 0);
66 ret = *ptr;
67 *ptr += add;
69 i = pthread_mutex_unlock (&sync_lock);
70 __go_assert (i == 0);
72 return ret;
75 #endif
77 // Called to initialize a new m (including the bootstrap m).
78 void
79 runtime_minit(void)
81 byte* stack;
82 size_t stacksize;
83 stack_t ss;
85 // Initialize signal handling.
86 runtime_m()->gsignal = runtime_malg(32*1024, &stack, &stacksize); // OS X wants >=8K, Linux >=2K
87 ss.ss_sp = stack;
88 ss.ss_flags = 0;
89 ss.ss_size = stacksize;
90 if(sigaltstack(&ss, nil) < 0)
91 *(int *)0xf1 = 0xf1;