Skip to content

Commit

Permalink
Don't use a pthread_t as a normal simple type.
Browse files Browse the repository at this point in the history
Not on all platforms a pthread_t is a simple type and as such you cannot
normally determine if its initialized or not. So for the allocation of
the SD message channel we now use a bool that gets set when the thread
is running and needs teardown.
  • Loading branch information
Marco van Wieringen committed Jan 27, 2014
1 parent e91c11e commit 46e642e
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/dird/backup.c
Expand Up @@ -735,7 +735,7 @@ void native_backup_cleanup(JCR *jcr, int TermCode)
msg_type = M_ERROR; /* Generate error message */
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand All @@ -744,7 +744,7 @@ void native_backup_cleanup(JCR *jcr, int TermCode)
term_msg = _("Backup Canceled");
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/dird/dird.c
Expand Up @@ -162,8 +162,7 @@ PROG_COPYRIGHT
*
*/
#if defined(HAVE_WIN32)
/* For Win32 main() is in src/win32 code ... */
#define main BAREOSMain
#define main BareosMain
#endif

int main (int argc, char *argv[])
Expand Down
2 changes: 1 addition & 1 deletion src/dird/job.c
Expand Up @@ -513,7 +513,7 @@ void sd_msg_thread_send_signal(JCR *jcr, int sig)
{
jcr->lock();
if (!jcr->sd_msg_thread_done &&
jcr->SD_msg_chan &&
jcr->SD_msg_chan_started &&
!pthread_equal(jcr->SD_msg_chan, pthread_self())) {
Dmsg1(800, "Send kill to SD msg chan jid=%d\n", jcr->JobId);
pthread_kill(jcr->SD_msg_chan, sig);
Expand Down
4 changes: 2 additions & 2 deletions src/dird/migrate.c
Expand Up @@ -1605,7 +1605,7 @@ void migration_cleanup(JCR *jcr, int TermCode)
*/
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand All @@ -1615,7 +1615,7 @@ void migration_cleanup(JCR *jcr, int TermCode)
*/
if (mig_jcr->store_bsock) {
mig_jcr->store_bsock->signal(BNET_TERMINATE);
if (mig_jcr->SD_msg_chan) {
if (mig_jcr->SD_msg_chan_started) {
pthread_cancel(mig_jcr->SD_msg_chan);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/dird/msgchan.c
Expand Up @@ -354,14 +354,14 @@ bool start_storage_daemon_message_thread(JCR *jcr)

jcr->inc_use_count(); /* mark in use by msg thread */
jcr->sd_msg_thread_done = false;
jcr->SD_msg_chan = 0;
jcr->SD_msg_chan_started = false;
Dmsg0(100, "Start SD msg_thread.\n");
if ((status = pthread_create(&thid, NULL, msg_thread, (void *)jcr)) != 0) {
berrno be;
Jmsg1(jcr, M_ABORT, 0, _("Cannot create message thread: %s\n"), be.bstrerror(status));
}
/* Wait for thread to start */
while (jcr->SD_msg_chan == 0) {
while (!jcr->SD_msg_chan_started) {
bmicrosleep(0, 50);
if (job_canceled(jcr) || jcr->sd_msg_thread_done) {
return false;
Expand All @@ -378,7 +378,7 @@ extern "C" void msg_thread_cleanup(void *arg)
db_end_transaction(jcr, jcr->db); /* terminate any open transaction */
jcr->lock();
jcr->sd_msg_thread_done = true;
jcr->SD_msg_chan = 0;
jcr->SD_msg_chan_started = false;
jcr->unlock();
pthread_cond_broadcast(&jcr->nextrun_ready); /* wakeup any waiting threads */
pthread_cond_broadcast(&jcr->term_wait); /* wakeup any waiting threads */
Expand Down Expand Up @@ -406,6 +406,7 @@ extern "C" void *msg_thread(void *arg)
pthread_detach(pthread_self());
set_jcr_in_tsd(jcr);
jcr->SD_msg_chan = pthread_self();
jcr->SD_msg_chan_started = true;
pthread_cleanup_push(msg_thread_cleanup, arg);
sd = jcr->store_bsock;

Expand Down Expand Up @@ -480,7 +481,7 @@ void wait_for_storage_daemon_termination(JCR *jcr)
pthread_cond_timedwait(&jcr->term_wait, &mutex, &timeout);
V(mutex);
if (jcr->is_canceled()) {
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
jcr->store_bsock->set_timed_out();
jcr->store_bsock->set_terminated();
sd_msg_thread_send_signal(jcr, TIMEOUT_SIGNAL);
Expand Down
8 changes: 4 additions & 4 deletions src/dird/ndmp_dma.c
Expand Up @@ -2058,7 +2058,7 @@ void ndmp_backup_cleanup(JCR *jcr, int TermCode)
msg_type = M_ERROR; /* Generate error message */
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand All @@ -2067,7 +2067,7 @@ void ndmp_backup_cleanup(JCR *jcr, int TermCode)
term_msg = _("Backup Canceled");
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand Down Expand Up @@ -2516,7 +2516,7 @@ void ndmp_restore_cleanup(JCR *jcr, int TermCode)
msg_type = M_ERROR; /* Generate error message */
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand All @@ -2525,7 +2525,7 @@ void ndmp_restore_cleanup(JCR *jcr, int TermCode)
term_msg = _("Restore Canceled");
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dird/restore.c
Expand Up @@ -482,7 +482,7 @@ void native_restore_cleanup(JCR *jcr, int TermCode)
msg_type = M_ERROR; /* Generate error message */
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand All @@ -491,7 +491,7 @@ void native_restore_cleanup(JCR *jcr, int TermCode)
term_msg = _("Restore Canceled");
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/dird/vbackup.c
Expand Up @@ -347,7 +347,7 @@ void native_vbackup_cleanup(JCR *jcr, int TermCode)
msg_type = M_ERROR; /* Generate error message */
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand All @@ -356,7 +356,7 @@ void native_vbackup_cleanup(JCR *jcr, int TermCode)
term_msg = _("Backup Canceled");
if (jcr->store_bsock) {
jcr->store_bsock->signal(BNET_TERMINATE);
if (jcr->SD_msg_chan) {
if (jcr->SD_msg_chan_started) {
pthread_cancel(jcr->SD_msg_chan);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/include/jcr.h
Expand Up @@ -413,6 +413,7 @@ class JCR {
* Director Daemon specific data part of JCR
*/
pthread_t SD_msg_chan; /* Message channel thread id */
bool SD_msg_chan_started; /* Message channel thread started */
pthread_cond_t term_wait; /* Wait for job termination */
pthread_cond_t nextrun_ready; /* Wait for job next run to become ready */
workq_ele_t *work_item; /* Work queue item if scheduled */
Expand Down

0 comments on commit 46e642e

Please sign in to comment.