static int cpu_avg_samples, net_avg_samples;
-pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
-pthread_t execthread = (pthread_t)0;
-
#ifdef X11
/* Always on bottom */
static struct special_t *new_special(char *buf, int t)
{
- if (special_count >= 128)
+ if (special_count >= 256)
CRIT_ERR("too many special things in text");
buf[0] = SPECIAL_CHAR;
#endif
};
+struct thread_info_s {
+ pthread_mutex_t mutex;
+ pthread_t thread;
+};
+
struct text_object {
int type;
int a, b;
char *cmd;
char *buffer;
double data;
+ int pos;
+ struct thread_info_s thread_info;
} execi; /* 5 */
struct {
static struct text_object *text_objects;
static void generate_text_internal(char *p, int p_max_size, struct text_object *objs, unsigned int object_count, struct information *cur);
-void *threaded_exec( struct text_object *obj ) {
- pthread_mutex_lock( &mutex1 );
+#define MAX_THREADS 512 // sure whatever
+typedef struct thread_info_s *thread_info;
+thread_info thread_list[MAX_THREADS];
+int thread_count = 0;
+
+int register_thread(struct thread_info_s *new_thread)
+{
+ if (thread_count >= MAX_THREADS) {
+ CRIT_ERR("Uh oh, tried to register too many threads");
+ } else {
+ thread_list[thread_count] = new_thread;
+ thread_count++;
+ }
+ return thread_count - 1;
+}
+
+void print_shit()
+{
+ int i;
+ for (i = 0; i < thread_count; i++) {
+ printf("thread is %i\n", (int)(thread_list[i])->thread);
+ }
+
+}
+
+void replace_thread(struct thread_info_s *new_thread, int pos)
+{
+ if (pos >= 0 && pos < MAX_THREADS) {
+ thread_list[pos] = new_thread;
+ } else {
+ ERR("thread position out of bounds");
+ }
+}
+
+void join_all_threads()
+{
+ int i;
+ // now we wait to get the locks
+ for (i = 0; i < thread_count; i++) {
+ // try to lock mutex
+ if (thread_list[i]) {
+ if (thread_list[i]->thread) {
+ printf("thread is %i\n", (int)thread_list[i]->thread);
+/* pthread_mutex_lock(&(thread_list[i]->mutex));
+ if (pthread_join(thread_list[i]->thread, NULL))
+ ERR("Problem joining thread %i\n", (int)thread_list[i]->thread);*/
+ }
+ }
+ }
+}
+
+void *threaded_exec(struct text_object *obj) {
+ pthread_mutex_lock(&(obj->data.execi.thread_info.mutex));
char *p2 = obj->data.execi.buffer;
FILE *fp = popen(obj->data.execi.cmd,"r");
int n2 = fread(p2, 1, TEXT_BUFFER_SIZE, fp);
p2++;
}
obj->data.execi.last_update = current_update_time;
- pthread_mutex_unlock( &mutex1 );
+ pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
pthread_exit(NULL);
}
}
END OBJ(texeci, 0) unsigned int n;
- if (!arg
- || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
+ if (!arg || sscanf(arg, "%f %n", &obj->data.execi.interval, &n) <= 0) {
char buf[256];
ERR("${texeci <interval> command}");
obj->type = OBJ_text;
obj->data.execi.buffer =
(char *) calloc(1, TEXT_BUFFER_SIZE);
}
+ pthread_mutex_init(&(obj->data.execi.thread_info.mutex), NULL);
+ obj->data.execi.pos = register_thread(&(obj->data.execi.thread_info));
END OBJ(pre_exec, 0) obj->type = OBJ_text;
if (arg) {
FILE *fp = popen(arg, "r");
if (current_update_time - obj->data.execi.last_update < obj->data.execi.interval) {
snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
} else {
- if (!execthread && !pthread_mutex_trylock(&mutex1)) {
+ if (!obj->data.execi.thread_info.thread && !pthread_mutex_trylock(&(obj->data.execi.thread_info.mutex))) {
- if (pthread_create(&execthread, NULL, (void*)threaded_exec, (void*) obj)) {
+ if (pthread_create(&(obj->data.execi.thread_info.thread), NULL, (void*)threaded_exec, (void*) obj)) {
ERR("Error starting thread");
}
- pthread_mutex_unlock(&mutex1);
+// printf("thread is %i\n", (int)obj->data.execi.thread_info.thread);
+ pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
}
snprintf(p, p_max_size, "%s", obj->data.execi.buffer);
}
- if (!pthread_mutex_trylock(&mutex1)) {
- // pthread_join(execthread, NULL);
- execthread = (pthread_t)0;
- pthread_mutex_unlock(&mutex1);
+ if (!pthread_mutex_trylock(&(obj->data.execi.thread_info.mutex))) {
+ obj->data.execi.thread_info.thread = (pthread_t)0;
+ pthread_mutex_unlock(&(obj->data.execi.thread_info.mutex));
}
//parse_conky_vars(obj->data.execi.buffer, p, cur);
}
info.looped = 0;
while (total_run_times == 0 || info.looped < total_run_times - 1) {
info.looped++;
+ printf("loop %i:\n", (int)info.looped);
+ print_shit();
#ifdef SIGNAL_BLOCKING
/* block signals. we will inspect for pending signals later */
/* reload the config file */
void reload_config(void)
{
- pthread_mutex_lock(&mutex1);
+ join_all_threads();
#if defined(XMMS) || defined(BMP) || defined(AUDACIOUS) || defined(INFOPIPE)
if (info.xmms.thread) {
if (destroy_xmms_thread()!=0)
text = NULL;
update_text();
}
- pthread_mutex_unlock(&mutex1);
+ thread_count = 0;
}
void clean_up(void)
{
- pthread_mutex_lock(&mutex1);
- execthread = (pthread_t)0;
+ join_all_threads();
#ifdef X11
#ifdef XDBE
if (use_xdbe) {