Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / toolchain / cpp_threadsafe_static_init.cc
1 /*
2  * Copyright (c) 2014 The Native Client Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6
7 #include <pthread.h>
8 #include <stdio.h>
9 #include <unistd.h>
10
11 #include "native_client/src/include/nacl_assert.h"
12
13
14 class ClassWithInitializer {
15  public:
16   ClassWithInitializer() {
17     // Sleep for 0.1 seconds.  This should be long enough for both threads
18     // to enter the static initializer at the same time.
19     struct timespec time = { 0, 100000000 };
20     int rc = nanosleep(&time, NULL);
21     ASSERT_EQ(rc, 0);
22   }
23 };
24
25 static void *thread_func(void *arg) {
26   static ClassWithInitializer obj;
27   printf("Initialised object %p\n", (void *) &obj);
28   return NULL;
29 }
30
31 int main() {
32   pthread_t tid;
33   int rc = pthread_create(&tid, NULL, thread_func, NULL);
34   ASSERT_EQ(rc, 0);
35
36   thread_func(NULL);
37
38   rc = pthread_join(tid, NULL);
39   ASSERT_EQ(rc, 0);
40   return 0;
41 }