00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00024 #include "config.h"
00025
00026 #include <cstring>
00027
00028 #include <scalestack/kernel/module.h>
00029 #include <scalestack/kernel/option.h>
00030 #include <scalestack/threading/pthreads/thread_provider.h>
00031
00032 namespace scalestack
00033 {
00034 namespace threading
00035 {
00036 namespace pthreads
00037 {
00038
00039
00040
00041
00042
00043 extern "C" void* thread_run(void* context)
00044 {
00045 thread_provider* thread_provider =
00046 static_cast<pthreads::thread_provider*>(context);
00047 thread_provider->run();
00048 return NULL;
00049 }
00050
00051
00052
00053
00054
00055 thread_provider::thread_provider(kernel::module& module, thread& thread):
00056 threading::thread_provider(module, thread),
00057 _thread_id()
00058 {
00059 }
00060
00061 void thread_provider::start(void)
00062 {
00063 pthread_attr_t thread_attribute;
00064
00065 int return_code = pthread_attr_init(&thread_attribute);
00066 if (return_code != 0)
00067 {
00068 _module.log_fatal(_("Failed pthread_attr_init: %s:%d"),
00069 strerror(return_code), return_code);
00070 }
00071
00072 size_t stack_size = _module.get_option("thread_stack_size").get_size_value();
00073 if (stack_size != 0)
00074 {
00075 return_code = pthread_attr_setstacksize(&thread_attribute, stack_size);
00076 if (return_code != 0)
00077 {
00078 (void) pthread_attr_destroy(&thread_attribute);
00079 _module.log_fatal(_("Failed pthread_attr_setstacksize: %s:%d"),
00080 strerror(return_code), return_code);
00081 }
00082 }
00083
00084 return_code = pthread_attr_setscope(&thread_attribute, PTHREAD_SCOPE_SYSTEM);
00085 if (return_code != 0)
00086 {
00087 (void) pthread_attr_destroy(&thread_attribute);
00088 _module.log_fatal(_("Failed pthread_attr_setscope: %s:%d"),
00089 strerror(return_code), return_code);
00090 }
00091
00092 return_code = pthread_create(&_thread_id, &thread_attribute, thread_run,
00093 this);
00094 if (return_code != 0)
00095 {
00096 (void) pthread_attr_destroy(&thread_attribute);
00097 _module.log_fatal(_("Failed pthread_create: %s:%d"),
00098 strerror(return_code), return_code);
00099 }
00100
00101 return_code = pthread_attr_destroy(&thread_attribute);
00102 if (return_code != 0)
00103 {
00104 _module.log_error(_("Failed pthread_attr_destroy: %s:%d"),
00105 strerror(return_code), return_code);
00106 }
00107
00108 _module.log_info("[Thread %u] Started", get_id());
00109 }
00110
00111 void thread_provider::stop(void)
00112 {
00113 int return_code = pthread_join(_thread_id, NULL);
00114 if (return_code != 0)
00115 {
00116 _module.log_error(_("Failed pthread_join: %s:%d"),
00117 strerror(return_code), return_code);
00118 }
00119
00120 _module.log_info("[Thread %u] Stopped", get_id());
00121 }
00122
00123 size_t thread_provider::get_id()
00124 {
00125 return static_cast<size_t>(_thread_id);
00126 }
00127
00128 }
00129 }
00130 }