blob: def4aa50903af85096db6a7b5530e9895ef464ba [file] [log] [blame]
Sharvil Nanavati118bdd52014-05-07 22:09:12 -07001/******************************************************************************
2 *
3 * Copyright (C) 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19#pragma once
20
Sharvil Nanavati7c19f012014-08-14 12:36:39 -070021#include <stdbool.h>
Chris Mantonbc93d572015-01-07 13:54:24 -080022#include <stdlib.h>
Sharvil Nanavati7c19f012014-08-14 12:36:39 -070023
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070024#define THREAD_NAME_MAX 16
25
Sharvil Nanavati9d461af2014-07-24 14:24:27 -070026typedef struct reactor_t reactor_t;
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070027typedef struct thread_t thread_t;
Sharvil Nanavati94347a52014-07-04 17:51:29 -070028typedef void (*thread_fn)(void *context);
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070029
Sharvil Nanavati94347a52014-07-04 17:51:29 -070030// Creates and starts a new thread with the given name. Only THREAD_NAME_MAX
31// bytes from |name| will be assigned to the newly-created thread. Returns a
32// thread object if the thread was successfully started, NULL otherwise. The
33// returned thread object must be freed with |thread_free|. |name| may not
34// be NULL.
35thread_t *thread_new(const char *name);
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070036
Chris Manton7307d702014-08-05 11:10:12 -070037// Similar to |thread_new| but creates with a given queue |size|.
38thread_t *thread_new_sized(const char *name, size_t size);
39
Sharvil Nanavati94347a52014-07-04 17:51:29 -070040// Frees the given |thread|. If the thread is still running, it is stopped
41// and the calling thread will block until |thread| terminates. |thread|
42// may be NULL.
43void thread_free(thread_t *thread);
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070044
Zach Johnson40fbe5d2014-08-15 16:43:32 -070045// Waits for |thread_stop|. Upon returning, the only other operations a caller
46// may perform on |thread| are |thread_free| and |thread_join|. |thread_join|
47// is idempotent and may be called from any thread. |thread| may not be NULL.
48void thread_join(thread_t *thread);
49
Sharvil Nanavati94347a52014-07-04 17:51:29 -070050// Call |func| with the argument |context| on |thread|. This function typically
51// does not block unless there are an excessive number of functions posted to
52// |thread| that have not been dispatched yet. Neither |thread| nor |func| may
53// be NULL. |context| may be NULL.
54bool thread_post(thread_t *thread, thread_fn func, void *context);
55
56// Requests |thread| to stop. Only |thread_free| and |thread_name| may be called
57// after calling |thread_stop|. This function is guaranteed to not block.
58// |thread| may not be NULL.
59void thread_stop(thread_t *thread);
60
Sharvil Nanavati7c19f012014-08-14 12:36:39 -070061// Returns true if the current thread is the same as the one represented by |thread|.
62// |thread| may not be NULL.
63bool thread_is_self(const thread_t *thread);
64
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070065reactor_t *thread_get_reactor(const thread_t *thread);
66
Sharvil Nanavati94347a52014-07-04 17:51:29 -070067// Returns the name of the given |thread|. |thread| may not be NULL.
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070068const char *thread_name(const thread_t *thread);