blob: c8da38106b3a39e8a05ad92865113dd9f2b93e61 [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;
Jakub Pawlowski713993d2016-04-21 13:16:45 -070028
Myles Watsonb55040c2016-10-19 13:15:34 -070029typedef void (*thread_fn)(void* context);
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070030
Sharvil Nanavati94347a52014-07-04 17:51:29 -070031// Creates and starts a new thread with the given name. Only THREAD_NAME_MAX
32// bytes from |name| will be assigned to the newly-created thread. Returns a
33// thread object if the thread was successfully started, NULL otherwise. The
34// returned thread object must be freed with |thread_free|. |name| may not
35// be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070036thread_t* thread_new(const char* name);
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070037
Chris Manton7307d702014-08-05 11:10:12 -070038// Similar to |thread_new| but creates with a given queue |size|.
Myles Watsonb55040c2016-10-19 13:15:34 -070039thread_t* thread_new_sized(const char* name, size_t size);
Chris Manton7307d702014-08-05 11:10:12 -070040
Sharvil Nanavati94347a52014-07-04 17:51:29 -070041// Frees the given |thread|. If the thread is still running, it is stopped
42// and the calling thread will block until |thread| terminates. |thread|
43// may be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070044void thread_free(thread_t* thread);
Sharvil Nanavati118bdd52014-05-07 22:09:12 -070045
Zach Johnson40fbe5d2014-08-15 16:43:32 -070046// Waits for |thread_stop|. Upon returning, the only other operations a caller
47// may perform on |thread| are |thread_free| and |thread_join|. |thread_join|
48// is idempotent and may be called from any thread. |thread| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070049void thread_join(thread_t* thread);
Zach Johnson40fbe5d2014-08-15 16:43:32 -070050
Sharvil Nanavati94347a52014-07-04 17:51:29 -070051// Call |func| with the argument |context| on |thread|. This function typically
52// does not block unless there are an excessive number of functions posted to
53// |thread| that have not been dispatched yet. Neither |thread| nor |func| may
54// be NULL. |context| may be NULL.
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080055// Return true on success, otherwise false.
Myles Watsonb55040c2016-10-19 13:15:34 -070056bool thread_post(thread_t* thread, thread_fn func, void* context);
Sharvil Nanavati94347a52014-07-04 17:51:29 -070057
58// Requests |thread| to stop. Only |thread_free| and |thread_name| may be called
59// after calling |thread_stop|. This function is guaranteed to not block.
60// |thread| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070061void thread_stop(thread_t* thread);
Sharvil Nanavati94347a52014-07-04 17:51:29 -070062
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -070063// Attempts to sets the |priority| of a given |thread|.
64// The |thread| has to be running for this call to succeed.
65// Returns true on success.
Myles Watsonb55040c2016-10-19 13:15:34 -070066bool thread_set_priority(thread_t* thread, int priority);
Andre Eisenbach6c25b3c2015-10-07 11:16:37 -070067
Philip Cuadraeaa42772017-03-23 10:10:34 -070068// Attempts to set |thread| to the real-time SCHED_FIFO |priority|.
69// The |thread| has to be running for this call to succeed.
70// Priority values are valid in the range sched_get_priority_max(SCHED_FIFO)
71// to sched_get_priority_min(SCHED_FIFO). Larger values are higher priority.
72// Returns true on success.
73bool thread_set_rt_priority(thread_t* thread, int priority);
74
Myles Watsonb55040c2016-10-19 13:15:34 -070075// Returns true if the current thread is the same as the one represented by
76// |thread|.
Sharvil Nanavati7c19f012014-08-14 12:36:39 -070077// |thread| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070078bool thread_is_self(const thread_t* thread);
Sharvil Nanavati7c19f012014-08-14 12:36:39 -070079
Pavlin Radoslavov78bcff72015-12-04 17:36:34 -080080// Returns the reactor for the given |thread|. |thread| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070081reactor_t* thread_get_reactor(const thread_t* thread);
Sharvil Nanavatifbf89082014-08-13 00:40:49 -070082
Sharvil Nanavati94347a52014-07-04 17:51:29 -070083// Returns the name of the given |thread|. |thread| may not be NULL.
Myles Watsonb55040c2016-10-19 13:15:34 -070084const char* thread_name(const thread_t* thread);