summaryrefslogtreecommitdiff
path: root/src/components/include/utils/threads/thread.h
diff options
context:
space:
mode:
authorJustin Dickow <jjdickow@gmail.com>2015-02-20 09:11:18 -0500
committerJustin Dickow <jjdickow@gmail.com>2015-02-20 09:11:18 -0500
commita7c5d752cb75485baa0ded5226335d0f8eb10321 (patch)
treefbfd9251ada2cdcd5cf6a03a79887d08f6b496d7 /src/components/include/utils/threads/thread.h
parentb2b2233d866f102d3de339afa8ccaf37d3cf2570 (diff)
downloadsdl_core-a7c5d752cb75485baa0ded5226335d0f8eb10321.tar.gz
Bug Fixes and ImprovementsSynchronizationCommit
Fix Empty perform iteration request Fix type of name from string to enum SendLocation implemented on HTML5 HMI Fixed PI response on VR rejection due to high priority. Fix Apps not responsive/not able to start app/apps remain listed on SYNC even after USB disconnect Mobile API change and processing capabilities Change perform interaction request conditions. Fix SDL must always start 3sec timer before resuming the HMILevel of the app Remove redundant StartSavePersistentDataTimer() call. Change wrong predicate name to right. Added stream request handling feature Made streaming timeout in media manager configurable Put navi app in LIMITED in case of phone call Handling of audio state for applications Add stop streaming timeout into ini file Implement HMILevel resumption for job-1 Fix result code ABORTED when interrupts it by Voice recognition activation Fix incorrect value parameter unexpectedDisconnect in BCOnAppUnregistered Fix SDL send BC.OnAppUnregistered with "unexpectedDisconnect" set to "true" in case received from HMI OnExitAllApplications {"reason":"MASTER_RESET"} Fix Update ini file for iAP1 support Current working directory added to image path Fix helpers to make it workable with more then 2 parameters DCHECK() for ManageMobileCommand() replaced with log message because the latter returns false in some regular situations (e.g. TOO_MANY_PENDING_REQUESTS, see SDLAQ-CRS-10) Remove connection after closing. Signed-off-by: Justin Dickow <jjdickow@gmail.com>
Diffstat (limited to 'src/components/include/utils/threads/thread.h')
-rw-r--r--src/components/include/utils/threads/thread.h146
1 files changed, 72 insertions, 74 deletions
diff --git a/src/components/include/utils/threads/thread.h b/src/components/include/utils/threads/thread.h
index 3b81cf3454..fd2b5e9fd9 100644
--- a/src/components/include/utils/threads/thread.h
+++ b/src/components/include/utils/threads/thread.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2014, Ford Motor Company
+ * Copyright (c) 2015, Ford Motor Company
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -43,19 +43,19 @@
#include "utils/macro.h"
#include "utils/threads/thread_delegate.h"
#include "utils/threads/thread_options.h"
+#include "utils/conditional_variable.h"
+#include "utils/lock.h"
namespace threads {
-namespace impl {
#if defined(OS_POSIX)
typedef pthread_t PlatformThreadHandle;
#else
#error Please implement thread for your OS
#endif
-}
/**
- * Non platform specific thread abstraction that establishes a
+ * @brief Non platform specific thread abstraction that establishes a
* threads::ThreadDelegate on a new thread.
*
* ThreadDelegate example:
@@ -75,60 +75,72 @@ typedef pthread_t PlatformThreadHandle;
* thread.join();
* printf("ok!\n");
*/
+
class Thread;
+void enqueue_to_join(Thread* thread);
+
Thread* CreateThread(const char* name, ThreadDelegate* delegate);
-void DeleteThread(Thread*);
+void DeleteThread(Thread* thread);
class Thread {
- friend Thread* CreateThread(const char*, ThreadDelegate*);
- friend void DeleteThread(Thread*);
- public:
- /**
- * Class that represents unique in-process thread identifier
- * due to restriction of pthread API it only allows checks
- * for equality to different thread id and no ordering.
- *
- * ostream<< operator is provided for this class which
- * outputs thread name associated to an identifier.
- */
- class Id {
- public:
- explicit Id(const impl::PlatformThreadHandle& id): id_(id) {}
- bool operator==(const Id& that) const;
- impl::PlatformThreadHandle Handle() const { return id_; }
- private:
- impl::PlatformThreadHandle id_;
- friend class Thread;
- };
-
- // Get unique ID of currently executing thread
- static Id CurrentId();
-
- // Get name associated with thread identified by thread_id
- static std::string NameFromId(const Id& thread_id);
-
- // Give thread thread_id a name, helpful for debugging
- static void SetNameForId(const Id& thread_id, const std::string& name);
+ private:
+ const std::string name_;
+ // Should be locked to protect delegate_ value
+ sync_primitives::Lock delegate_lock_;
+ ThreadDelegate* delegate_;
+ PlatformThreadHandle handle_;
+ ThreadOptions thread_options_;
+ // Should be locked to protect isThreadRunning_ and thread_created_ values
+ sync_primitives::Lock state_lock_;
+ volatile unsigned int isThreadRunning_;
+ volatile bool stopped_;
+ volatile bool finalized_;
+ bool thread_created_;
+ // Signalled when Thread::start() is called
+ sync_primitives::ConditionalVariable run_cond_;
+ public:
/**
- * Starts the thread.
+ * @brief Starts the thread.
* @return true if the thread was successfully started.
*/
bool start();
- ThreadDelegate* delegate() const;
-
/**
- * Starts the thread. Behaves exactly like Start in addition to
+ * @brief Starts the thread. Behaves exactly like \ref start() in addition to
* allow to override the default options.
- * @param options - thread options. Look for 'threads/thread_options.h'
+ * @param options Thread options. Look for 'threads/thread_options.h'
* for details.
* @return true if the thread was successfully started.
*/
- bool startWithOptions(const ThreadOptions& options);
+ bool start(const ThreadOptions& options);
+
+ sync_primitives::Lock& delegate_lock() {
+ return delegate_lock_;
+ }
+
+ ThreadDelegate *delegate() const {
+ return delegate_;
+ }
+
+ void set_delegate(ThreadDelegate *delegate) {
+ DCHECK(!isThreadRunning_);
+ delegate_ = delegate;
+ }
+
+ friend Thread* CreateThread(const char* name, ThreadDelegate* delegate);
+ friend void DeleteThread(Thread* thread);
+
+ public:
+ // Get unique ID of currently executing thread
+ static PlatformThreadHandle CurrentId();
+
+ // Give thread thread_id a name, helpful for debugging
+ static void SetNameForId(const PlatformThreadHandle& thread_id,
+ std::string name);
/**
- * Signals the thread to exit and returns once the thread has exited.
+ * @brief Signals the thread to exit and returns once the thread has exited.
* After this method returns, the Thread object is completely reset and may
* be used as if it were newly constructed (i.e., Start may be called again).
*
@@ -137,16 +149,18 @@ class Thread {
*/
void stop();
+ void join();
+
/**
- * Get thread name.
+ * @brief Get thread name.
* @return thread name
*/
- const std::string& thread_name() {
+ const std::string& name() {
return name_;
}
/**
- * Returns true if the thread has been started, and not yet stopped.
+ * @brief Returns true if the thread has been started, and not yet stopped.
* When a thread is running, the thread_id_ is non-zero.
* @return true if the thread has been started, and not yet stopped.
*/
@@ -154,12 +168,10 @@ class Thread {
return isThreadRunning_;
}
- void set_running(bool running) {
- isThreadRunning_ = running;
- }
+ void set_running(bool running);
/**
- * Is thread joinable?
+ * @brief Is thread joinable?
* @return - Returns true if the thread is joinable.
*/
bool is_joinable() const {
@@ -167,7 +179,7 @@ class Thread {
}
/**
- * Thread stack size
+ * @brief Thread stack size
* @return thread stack size
*/
size_t stack_size() const {
@@ -175,23 +187,15 @@ class Thread {
}
/**
- * The native thread handle.
+ * @brief The native thread handle.
* @return thread handle.
*/
- impl::PlatformThreadHandle thread_handle() const {
- return thread_handle_;
- }
-
- /**
- * Thread id.
- * @return return thread id.
- */
- Id thread_id() const {
- return Id(thread_handle());
+ PlatformThreadHandle thread_handle() const {
+ return handle_;
}
/**
- * Thread options.
+ * @brief Thread options.
* @return thread options.
*/
const ThreadOptions& thread_options() const {
@@ -199,16 +203,12 @@ class Thread {
}
/**
- * Minimum size of thread stack for specific platform.
+ * @brief Minimum size of thread stack for specific platform.
*/
static size_t kMinStackSize;
protected:
- const std::string name_;
- ThreadDelegate* delegate_;
- impl::PlatformThreadHandle thread_handle_;
- ThreadOptions thread_options_;
- volatile unsigned int isThreadRunning_;
+ sync_primitives::ConditionalVariable state_cond_;
private:
/**
@@ -216,19 +216,17 @@ class Thread {
* @param name - display string to identify the thread.
* @param delegate - thread procedure delegate. Look for
* 'threads/thread_delegate.h' for details.
- * NOTE: delegate will be deleted by destructor.
+ * LifeCycle thread , otherwise it will be joined in stop method
+ * NOTE: delegate will be deleted after thread will be joined
* This constructor made private to prevent
* Thread object to be created on stack
*/
Thread(const char* name, ThreadDelegate* delegate);
-
+ virtual ~Thread();
+ static void* threadFunc(void* arg);
+ static void cleanup(void* arg);
DISALLOW_COPY_AND_ASSIGN(Thread);
- virtual ~Thread() { }
};
-inline bool operator!= (const Thread::Id& left, const Thread::Id& right) {
- return !(left == right);
-}
-std::ostream& operator<<(std::ostream& os, const Thread::Id& thread_id);
} // namespace threads
#endif // SRC_COMPONENTS_INCLUDE_UTILS_THREADS_THREAD_H_