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 21 #define THREAD_NAME_MAX 16 22 23 typedef struct thread_t thread_t; 24 typedef void (*thread_fn)(void *context); 25 26 // Creates and starts a new thread with the given name. Only THREAD_NAME_MAX 27 // bytes from |name| will be assigned to the newly-created thread. Returns a 28 // thread object if the thread was successfully started, NULL otherwise. The 29 // returned thread object must be freed with |thread_free|. |name| may not 30 // be NULL. 31 thread_t *thread_new(const char *name); 32 33 // Frees the given |thread|. If the thread is still running, it is stopped 34 // and the calling thread will block until |thread| terminates. |thread| 35 // may be NULL. 36 void thread_free(thread_t *thread); 37 38 // Call |func| with the argument |context| on |thread|. This function typically 39 // does not block unless there are an excessive number of functions posted to 40 // |thread| that have not been dispatched yet. Neither |thread| nor |func| may 41 // be NULL. |context| may be NULL. 42 bool thread_post(thread_t *thread, thread_fn func, void *context); 43 44 // Requests |thread| to stop. Only |thread_free| and |thread_name| may be called 45 // after calling |thread_stop|. This function is guaranteed to not block. 46 // |thread| may not be NULL. 47 void thread_stop(thread_t *thread); 48 49 // Returns the name of the given |thread|. |thread| may not be NULL. 50 const char *thread_name(const thread_t *thread); 51