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.
8 #include <sys/resource.h>
11 #include "go-assert.h"
13 /* For targets which don't have the required sync support. Really
14 these should be provided by gcc itself. FIXME. */
16 #if !defined (HAVE_SYNC_BOOL_COMPARE_AND_SWAP_4) || !defined (HAVE_SYNC_BOOL_COMPARE_AND_SWAP_8) || !defined (HAVE_SYNC_FETCH_AND_ADD_4) || !defined (HAVE_SYNC_ADD_AND_FETCH_8)
18 static pthread_mutex_t sync_lock
= PTHREAD_MUTEX_INITIALIZER
;
22 #ifndef HAVE_SYNC_BOOL_COMPARE_AND_SWAP_4
25 __sync_bool_compare_and_swap_4 (uint32
*, uint32
, uint32
)
26 __attribute__ ((visibility ("hidden")));
29 __sync_bool_compare_and_swap_4 (uint32
* ptr
, uint32 old
, uint32
new)
34 i
= pthread_mutex_lock (&sync_lock
);
45 i
= pthread_mutex_unlock (&sync_lock
);
53 #ifndef HAVE_SYNC_BOOL_COMPARE_AND_SWAP_8
56 __sync_bool_compare_and_swap_8 (uint64
*, uint64
, uint64
)
57 __attribute__ ((visibility ("hidden")));
60 __sync_bool_compare_and_swap_8 (uint64
* ptr
, uint64 old
, uint64
new)
65 i
= pthread_mutex_lock (&sync_lock
);
76 i
= pthread_mutex_unlock (&sync_lock
);
84 #ifndef HAVE_SYNC_FETCH_AND_ADD_4
87 __sync_fetch_and_add_4 (uint32
*, uint32
)
88 __attribute__ ((visibility ("hidden")));
91 __sync_fetch_and_add_4 (uint32
* ptr
, uint32 add
)
96 i
= pthread_mutex_lock (&sync_lock
);
102 i
= pthread_mutex_unlock (&sync_lock
);
103 __go_assert (i
== 0);
110 #ifndef HAVE_SYNC_ADD_AND_FETCH_8
113 __sync_add_and_fetch_8 (uint64
*, uint64
)
114 __attribute__ ((visibility ("hidden")));
117 __sync_add_and_fetch_8 (uint64
* ptr
, uint64 add
)
122 i
= pthread_mutex_lock (&sync_lock
);
123 __go_assert (i
== 0);
128 i
= pthread_mutex_unlock (&sync_lock
);
129 __go_assert (i
== 0);
136 // Called to initialize a new m (including the bootstrap m).
145 // Initialize signal handling.
146 runtime_m()->gsignal
= runtime_malg(32*1024, &stack
, &stacksize
); // OS X wants >=8K, Linux >=2K
149 ss
.ss_size
= stacksize
;
150 if(sigaltstack(&ss
, nil
) < 0)
152 if (sigemptyset(&sigs
) != 0)
153 runtime_throw("sigemptyset");
154 sigprocmask(SIG_SETMASK
, &sigs
, nil
);
158 runtime_memlimit(void)
163 if(getrlimit(RLIMIT_AS
, &rl
) != 0)
165 if(rl
.rlim_cur
>= 0x7fffffff)
168 // Estimate our VM footprint excluding the heap.
169 // Not an exact science: use size of binary plus
170 // some room for thread stacks.
172 if(used
>= rl
.rlim_cur
)
175 // If there's not at least 16 MB left, we're probably
176 // not going to be able to do much. Treat as no limit.
178 if(rl
.rlim_cur
< (16<<20))
181 return rl
.rlim_cur
- used
;