| Sharvil Nanavati | 118bdd5 | 2014-05-07 22:09:12 -0700 | [diff] [blame] | 1 | /****************************************************************************** | 
|  | 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 Nanavati | 7c19f01 | 2014-08-14 12:36:39 -0700 | [diff] [blame] | 21 | #include <stdbool.h> | 
| Chris Manton | bc93d57 | 2015-01-07 13:54:24 -0800 | [diff] [blame] | 22 | #include <stdlib.h> | 
| Sharvil Nanavati | 7c19f01 | 2014-08-14 12:36:39 -0700 | [diff] [blame] | 23 |  | 
| Sharvil Nanavati | 118bdd5 | 2014-05-07 22:09:12 -0700 | [diff] [blame] | 24 | #define THREAD_NAME_MAX 16 | 
|  | 25 |  | 
| Sharvil Nanavati | 9d461af | 2014-07-24 14:24:27 -0700 | [diff] [blame] | 26 | typedef struct reactor_t reactor_t; | 
| Sharvil Nanavati | 118bdd5 | 2014-05-07 22:09:12 -0700 | [diff] [blame] | 27 | typedef struct thread_t thread_t; | 
| Jakub Pawlowski | 713993d | 2016-04-21 13:16:45 -0700 | [diff] [blame] | 28 |  | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 29 | typedef void (*thread_fn)(void* context); | 
| Sharvil Nanavati | 118bdd5 | 2014-05-07 22:09:12 -0700 | [diff] [blame] | 30 |  | 
| Sharvil Nanavati | 94347a5 | 2014-07-04 17:51:29 -0700 | [diff] [blame] | 31 | // 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 36 | thread_t* thread_new(const char* name); | 
| Sharvil Nanavati | 118bdd5 | 2014-05-07 22:09:12 -0700 | [diff] [blame] | 37 |  | 
| Chris Manton | 7307d70 | 2014-08-05 11:10:12 -0700 | [diff] [blame] | 38 | // Similar to |thread_new| but creates with a given queue |size|. | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 39 | thread_t* thread_new_sized(const char* name, size_t size); | 
| Chris Manton | 7307d70 | 2014-08-05 11:10:12 -0700 | [diff] [blame] | 40 |  | 
| Sharvil Nanavati | 94347a5 | 2014-07-04 17:51:29 -0700 | [diff] [blame] | 41 | // 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 44 | void thread_free(thread_t* thread); | 
| Sharvil Nanavati | 118bdd5 | 2014-05-07 22:09:12 -0700 | [diff] [blame] | 45 |  | 
| Zach Johnson | 40fbe5d | 2014-08-15 16:43:32 -0700 | [diff] [blame] | 46 | // 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 49 | void thread_join(thread_t* thread); | 
| Zach Johnson | 40fbe5d | 2014-08-15 16:43:32 -0700 | [diff] [blame] | 50 |  | 
| Sharvil Nanavati | 94347a5 | 2014-07-04 17:51:29 -0700 | [diff] [blame] | 51 | // 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 Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 55 | // Return true on success, otherwise false. | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 56 | bool thread_post(thread_t* thread, thread_fn func, void* context); | 
| Sharvil Nanavati | 94347a5 | 2014-07-04 17:51:29 -0700 | [diff] [blame] | 57 |  | 
|  | 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 61 | void thread_stop(thread_t* thread); | 
| Sharvil Nanavati | 94347a5 | 2014-07-04 17:51:29 -0700 | [diff] [blame] | 62 |  | 
| Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 63 | // 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 Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 66 | bool thread_set_priority(thread_t* thread, int priority); | 
| Andre Eisenbach | 6c25b3c | 2015-10-07 11:16:37 -0700 | [diff] [blame] | 67 |  | 
| Philip Cuadra | eaa4277 | 2017-03-23 10:10:34 -0700 | [diff] [blame^] | 68 | // 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. | 
|  | 73 | bool thread_set_rt_priority(thread_t* thread, int priority); | 
|  | 74 |  | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 75 | // Returns true if the current thread is the same as the one represented by | 
|  | 76 | // |thread|. | 
| Sharvil Nanavati | 7c19f01 | 2014-08-14 12:36:39 -0700 | [diff] [blame] | 77 | // |thread| may not be NULL. | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 78 | bool thread_is_self(const thread_t* thread); | 
| Sharvil Nanavati | 7c19f01 | 2014-08-14 12:36:39 -0700 | [diff] [blame] | 79 |  | 
| Pavlin Radoslavov | 78bcff7 | 2015-12-04 17:36:34 -0800 | [diff] [blame] | 80 | // Returns the reactor for the given |thread|. |thread| may not be NULL. | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 81 | reactor_t* thread_get_reactor(const thread_t* thread); | 
| Sharvil Nanavati | fbf8908 | 2014-08-13 00:40:49 -0700 | [diff] [blame] | 82 |  | 
| Sharvil Nanavati | 94347a5 | 2014-07-04 17:51:29 -0700 | [diff] [blame] | 83 | // Returns the name of the given |thread|. |thread| may not be NULL. | 
| Myles Watson | b55040c | 2016-10-19 13:15:34 -0700 | [diff] [blame] | 84 | const char* thread_name(const thread_t* thread); |