Skip to content

Commit

Permalink
Fixed X11 synchronization when message queue is
Browse files Browse the repository at this point in the history
enabled and synchronize access to the 'waiting_for_input' variable
  • Loading branch information
tarruda committed Jan 14, 2014
1 parent 6a4cefd commit b930d1d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ gui_start()

++recursive;

#if defined(FEAT_MESSAGEQUEUE) && defined(FEAT_GUI_X11)
#if defined(FEAT_MESSAGEQUEUE) && defined(FEAT_X11)
if (recursive <= 1) XInitThreads();
#endif

Expand Down
1 change: 0 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ main
#ifdef STARTUPTIME
int i;
#endif

/*
* Do any system-specific initialisations. These can NOT use IObuff or
* NameBuff. Thus emsg2() cannot be called!
Expand Down
17 changes: 15 additions & 2 deletions src/message_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ typedef struct message_queue_T

message_queue_T message_queue;

int waiting_for_input;
pthread_t input_thread;
pthread_mutex_t input_mutex;
pthread_cond_t input_cond;

int waiting_for_input;
pthread_mutex_t waiting_for_input_mutex;

/*
* FIXME: Figure out the right way to deal with such errors by asking
* on the list
Expand Down Expand Up @@ -69,8 +71,13 @@ input_wait()
void
input_notify()
{
if (waiting_for_input)
lock(&waiting_for_input_mutex);
if (waiting_for_input) {
unlock(&waiting_for_input_mutex);
return;
}
unlock(&waiting_for_input_mutex);


lock(&input_mutex);

Expand All @@ -93,13 +100,17 @@ vgetcs(arg)

while (TRUE)
{
lock(&waiting_for_input_mutex);
waiting_for_input = FALSE;
unlock(&waiting_for_input_mutex);

// Only try to read input when asked by the main thread
input_wait();

// Dont let the main thread call 'input_notify' or else it would block
lock(&waiting_for_input_mutex);
waiting_for_input = TRUE;
unlock(&waiting_for_input_mutex);

// Allocate space to hold input data
data = (input_data_T *)alloc(sizeof(input_data_T));
Expand Down Expand Up @@ -139,6 +150,8 @@ queue_init()
if (pthread_cond_init(&message_queue.cond, NULL) != 0)
pthread_error("Failed to init the condition");

if (pthread_mutex_init(&waiting_for_input_mutex, NULL) != 0)
pthread_error("Failed to init the mutex");

message_queue.head = NULL;
message_queue.tail = NULL;
Expand Down

0 comments on commit b930d1d

Please sign in to comment.