File: thread_run.c

package info (click to toggle)
gsql 0.2.2-1.2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,804 kB
  • sloc: ansic: 31,966; sh: 6,690; makefile: 866; xml: 706
file content (142 lines) | stat: -rw-r--r-- 3,233 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* 
 * GSQL - database development tool for GNOME
 *
 * Copyright (C) 2006-2008  Taras Halturin  halturin@gmail.com
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */



#include <libgsql/common.h>
#include <libgsql/workspace.h>
#include "plugin_runner.h"
#include "thread_run.h"
#include <unistd.h>


static gpointer
periodical_thread (gpointer data)
{
		
	GSQLPRunnerExecOptions *eopt = data;
	guint period;
	gchar mess[GSQL_MESSAGE_LEN];
	guint c, i, p;
	
	if (eopt->suspended)
	{
		GSQL_DEBUG ("Suspended period: %d", eopt->suspended_period);
		period = eopt->suspended_period;
		while (period)
		{
			if (!eopt->run_state)
			{
				GSQL_DEBUG ("stop from suspend");
				g_snprintf (mess, GSQL_MESSAGE_LEN, "Periodical execution stopped on suspend state [thread %d]",
							eopt->thread_id);
				gsql_message_add (eopt->workspace, GSQL_MESSAGE_NOTICE, mess);
				g_thread_exit (NULL);
			}
			period--;
			sleep(1);
			
		}
		
	}
	
	c = eopt->cycles;
	i = 0;
	
	while (c)
	{
		
		gsql_editor_run_sql (eopt->editor);
		GSQL_DEBUG ("c = %d",c);
		i++;
		
		p = eopt->period;
		while (p)
		{
			if (!eopt->run_state)
			{
				GSQL_DEBUG ("stop from run");
				g_snprintf (mess, GSQL_MESSAGE_LEN, "Periodical execution stopped [thread %d]"
							"\n\truns total: %d",
							eopt->thread_id, i);
				gsql_message_add (eopt->workspace, GSQL_MESSAGE_NOTICE, mess);
				g_thread_exit (NULL);
			}
		
			sleep(1);
			p--;
		}
		if (eopt->cycles_limit)
				c--;
	}
	
	g_snprintf (mess, GSQL_MESSAGE_LEN, "Periodical execution completed [thread %d]"
				"\n\truns total: %d",
				eopt->thread_id, i);
	gsql_message_add (eopt->workspace, GSQL_MESSAGE_NOTICE, mess);
	
	eopt->run_state = FALSE;
	gtk_toggle_action_set_active (eopt->action, FALSE);
	
	g_thread_exit (NULL);
}

void 
start_periodical (GSQLPRunnerExecOptions *eopt)
{
	
	GThread *thread = NULL;
	GError *err;
	gchar mess[GSQL_MESSAGE_LEN];
	guint s, c;
	static gint id = 1;
	
	thread = g_thread_create (periodical_thread,
							  eopt,
							  TRUE,
							  &err);
	if (!thread)
	{
		GSQL_DEBUG ("Couldn't create thread");
		return;
	}
	
	eopt->thread = thread;
	eopt->thread_id = id++;
	
	s = (eopt->suspended) ? eopt->suspended_period : 0;
	c = (eopt->cycles_limit) ? eopt->cycles : 0;
	g_snprintf (mess, GSQL_MESSAGE_LEN, 
				"Periodical execution started [thread: %d]..."
				"\n\tinterval: %d second[s]. "
				"\n\tsuspended: %d second[s]"
				"\n\tcycles: %d",
				eopt->thread_id, eopt->period, s, c);
	
	gsql_message_add (eopt->workspace, GSQL_MESSAGE_NOTICE, mess);
	
}