Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Give other threads a chance to lock the mutex in MQTTRun #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions MQTTClient-C/src/MQTTClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ int keepalive(MQTTClient* c)
TimerCountdownMS(&timer, 1000);
int len = MQTTSerialize_pingreq(c->buf, c->buf_size);
if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) // send the ping packet
{
c->ping_outstanding = 1;
TimerCountdown(&c->last_received, c->keepAliveInterval); // reset receive timer
}
}
}

Expand Down Expand Up @@ -293,8 +296,10 @@ int cycle(MQTTClient* c, Timer* timer)
len = MQTTSerialize_ack(c->buf, c->buf_size, PUBREC, 0, msg.id);
if (len <= 0)
rc = FAILURE;
else
else {
TimerCountdownMS(timer, c->command_timeout_ms);
rc = sendPacket(c, len, timer);
}
if (rc == FAILURE)
goto exit; // there was a problem
}
Expand All @@ -310,8 +315,11 @@ int cycle(MQTTClient* c, Timer* timer)
else if ((len = MQTTSerialize_ack(c->buf, c->buf_size,
(packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0)
rc = FAILURE;
else if ((rc = sendPacket(c, len, timer)) != SUCCESS) // send the PUBREL packet
rc = FAILURE; // there was a problem
else {
TimerCountdownMS(timer, c->command_timeout_ms);
if ((rc = sendPacket(c, len, timer)) != SUCCESS) // send the PUBREL packet
rc = FAILURE; // there was a problem
}
if (rc == FAILURE)
goto exit; // there was a problem
break;
Expand Down Expand Up @@ -372,13 +380,16 @@ void MQTTRun(void* parm)

while (1)
{
if (!c->isconnected)
continue;
#if defined(MQTT_TASK)
MutexLock(&c->mutex);
#endif
TimerCountdownMS(&timer, 500); /* Don't wait too long if no traffic is incoming */
cycle(c, &timer);
#if defined(MQTT_TASK)
MutexUnlock(&c->mutex);
for (TimerCountdownMS(&timer, 1); !TimerIsExpired(&timer);); /* Give other threads a chance to lock the mutex */
#endif
}
}
Expand Down