1/*
2 * Copyright (c) 2012 The Chromium 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 "ppapi/native_client/src/untrusted/irt_stub/thread_creator.h"
8
9#include <pthread.h>
10
11#include "native_client/src/include/nacl_macros.h"
12#include "native_client/src/untrusted/irt/irt.h"
13
14
15static int thread_create(uintptr_t *tid,
16                         void (*func)(void *thread_argument),
17                         void *thread_argument) {
18  /*
19   * We know that newlib and glibc use a small pthread_t type, so we
20   * do not need to wrap pthread_t values.
21   */
22  NACL_ASSERT_SAME_SIZE(pthread_t, uintptr_t);
23
24  return pthread_create((pthread_t *) tid, NULL,
25                        (void *(*)(void *thread_argument)) func,
26                        thread_argument);
27}
28
29static int thread_join(uintptr_t tid) {
30  return pthread_join((pthread_t) tid, NULL);
31}
32
33const static struct PP_ThreadFunctions thread_funcs = {
34  thread_create,
35  thread_join
36};
37
38/*
39 * We cannot tell at link time whether the application uses PPB_Audio,
40 * because of the way that PPAPI is defined via runtime interface
41 * query rather than a set of static functions.  This means that we
42 * register the audio thread functions unconditionally.  This adds the
43 * small overhead of pulling in pthread_create() even if the
44 * application does not use PPB_Audio or libpthread.
45 *
46 * If an application developer wants to avoid that cost, they can
47 * override this function with an empty definition.
48 */
49void __nacl_register_thread_creator(const struct nacl_irt_ppapihook *hooks) {
50  hooks->ppapi_register_thread_creator(&thread_funcs);
51}
52