Skip to content

Commit

Permalink
Changed to usec loop time on supported platforms. Cleaned up some unu…
Browse files Browse the repository at this point in the history
…sed variables.
  • Loading branch information
jakedouglas committed May 6, 2009
1 parent c4581c6 commit 91d5dc3
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 116 deletions.
8 changes: 4 additions & 4 deletions ext/cmain.cpp
Expand Up @@ -444,22 +444,22 @@ extern "C" const char *evma__write_file (const char *filename)
evma_get_comm_inactivity_timeout
********************************/

extern "C" int evma_get_comm_inactivity_timeout (const char *binding, int *value)
extern "C" float evma_get_comm_inactivity_timeout (const char *binding)
{
ensure_eventmachine("evma_get_comm_inactivity_timeout");
EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
if (ed) {
return ed->GetCommInactivityTimeout (value);
return ed->GetCommInactivityTimeout();
}
else
return 0; //Perhaps this should be an exception. Access to an unknown binding.
return 0.0; //Perhaps this should be an exception. Access to an unknown binding.
}

/********************************
evma_set_comm_inactivity_timeout
********************************/

extern "C" int evma_set_comm_inactivity_timeout (const char *binding, int *value)
extern "C" int evma_set_comm_inactivity_timeout (const char *binding, float value)
{
ensure_eventmachine("evma_set_comm_inactivity_timeout");
EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
Expand Down
78 changes: 16 additions & 62 deletions ext/ed.cpp
Expand Up @@ -48,8 +48,6 @@ EventableDescriptor::EventableDescriptor (int sd, EventMachine_t *em):
bCloseAfterWriting (false),
MySocket (sd),
EventCallback (NULL),
LastRead (0),
LastWritten (0),
bCallbackUnbind (true),
UnbindReasonCode (0),
MyEventMachine (em)
Expand Down Expand Up @@ -508,7 +506,6 @@ void ConnectionDescriptor::Read()

if (r > 0) {
total_bytes_read += r;
LastRead = gCurrentLoopTime;

// Add a null-terminator at the the end of the buffer
// that we will send to the callback.
Expand Down Expand Up @@ -1305,7 +1302,6 @@ void DatagramDescriptor::Read()

// In UDP, a zero-length packet is perfectly legal.
if (r >= 0) {
LastRead = gCurrentLoopTime;

// Add a null-terminator at the the end of the buffer
// that we will send to the callback.
Expand Down Expand Up @@ -1556,44 +1552,23 @@ bool ConnectionDescriptor::GetSockname (struct sockaddr *s)
ConnectionDescriptor::GetCommInactivityTimeout
**********************************************/

int ConnectionDescriptor::GetCommInactivityTimeout (int *value)
float ConnectionDescriptor::GetCommInactivityTimeout()
{
if (value) {
*value = InactivityTimeout;
return 1;
}
else {
// TODO, extended logging, got bad parameter.
return 0;
}
return ((float)InactivityTimeout / 1000000LL);
}


/**********************************************
ConnectionDescriptor::SetCommInactivityTimeout
**********************************************/

int ConnectionDescriptor::SetCommInactivityTimeout (int *value)
int ConnectionDescriptor::SetCommInactivityTimeout (float value)
{
int out = 0;

if (value) {
if ((*value==0) || (*value >= 2)) {
// Replace the value and send the old one back to the caller.
int v = *value;
*value = InactivityTimeout;
InactivityTimeout = v;
out = 1;
}
else {
// TODO, extended logging, got bad value.
}
}
else {
// TODO, extended logging, got bad parameter.
if (value > 0) {
InactivityTimeout = (Int64)(value * 1000000LL);
return 1;
}

return out;
return 0;
}

/*******************************
Expand Down Expand Up @@ -1633,43 +1608,22 @@ bool DatagramDescriptor::GetSockname (struct sockaddr *s)
DatagramDescriptor::GetCommInactivityTimeout
********************************************/

int DatagramDescriptor::GetCommInactivityTimeout (int *value)
float DatagramDescriptor::GetCommInactivityTimeout()
{
if (value) {
*value = InactivityTimeout;
return 1;
}
else {
// TODO, extended logging, got bad parameter.
return 0;
}
return ((float)InactivityTimeout / 1000000LL);
}

/********************************************
DatagramDescriptor::SetCommInactivityTimeout
********************************************/

int DatagramDescriptor::SetCommInactivityTimeout (int *value)
{
int out = 0;

if (value) {
if ((*value==0) || (*value >= 2)) {
// Replace the value and send the old one back to the caller.
int v = *value;
*value = InactivityTimeout;
InactivityTimeout = v;
out = 1;
}
else {
// TODO, extended logging, got bad value.
}
}
else {
// TODO, extended logging, got bad parameter.
}

return out;
int DatagramDescriptor::SetCommInactivityTimeout (float value)
{
if (value > 0) {
InactivityTimeout = (Int64)(value * 1000000LL);
return 1;
}
return 0;
}


Expand Down
31 changes: 14 additions & 17 deletions ext/ed.h
Expand Up @@ -73,10 +73,8 @@ class EventableDescriptor: public Bindable_t
virtual X509 *GetPeerCert() {return NULL;}
#endif

// Properties: return 0/1 to signify T/F, and handle the values
// through arguments.
virtual int GetCommInactivityTimeout (int *value) {return 0;}
virtual int SetCommInactivityTimeout (int *value) {return 0;}
virtual float GetCommInactivityTimeout() {return 0.0;}
virtual int SetCommInactivityTimeout (float value) {return 0;}

#ifdef HAVE_EPOLL
struct epoll_event *GetEpollEvent() { return &EpollEvent; }
Expand All @@ -96,14 +94,13 @@ class EventableDescriptor: public Bindable_t
//
// updating to 50 seconds, so we catch it before the OS does

PendingConnectTimeout = 50 // can easily be made an instance variable
// can easily be made an instance variable
PendingConnectTimeout = 50000000 // now in usec
};

void (*EventCallback)(const char*, int, const char*, int);

time_t CreatedAt;
time_t LastRead;
time_t LastWritten;

Int64 CreatedAt;
bool bCallbackUnbind;
int UnbindReasonCode;

Expand Down Expand Up @@ -180,8 +177,8 @@ class ConnectionDescriptor: public EventableDescriptor
virtual bool GetPeername (struct sockaddr*);
virtual bool GetSockname (struct sockaddr*);

virtual int GetCommInactivityTimeout (int *value);
virtual int SetCommInactivityTimeout (int *value);
virtual float GetCommInactivityTimeout();
virtual int SetCommInactivityTimeout (float value);


protected:
Expand Down Expand Up @@ -219,7 +216,7 @@ class ConnectionDescriptor: public EventableDescriptor
bool bGotExtraKqueueEvent;
#endif

time_t LastIo;
Int64 LastIo;
int InactivityTimeout;

private:
Expand Down Expand Up @@ -259,8 +256,8 @@ class DatagramDescriptor: public EventableDescriptor
virtual bool GetPeername (struct sockaddr*);
virtual bool GetSockname (struct sockaddr*);

virtual int GetCommInactivityTimeout (int *value);
virtual int SetCommInactivityTimeout (int *value);
virtual float GetCommInactivityTimeout();
virtual int SetCommInactivityTimeout (float value);

static int SendDatagram (const char*, const char*, int, const char*, int);

Expand All @@ -280,7 +277,7 @@ class DatagramDescriptor: public EventableDescriptor

struct sockaddr_in ReturnAddress;

time_t LastIo;
Int64 LastIo;
int InactivityTimeout;
};

Expand Down Expand Up @@ -341,7 +338,7 @@ class PipeDescriptor: public EventableDescriptor

protected:
bool bReadAttemptedAfterClose;
time_t LastIo;
Int64 LastIo;
int InactivityTimeout;

deque<OutboundPage> OutboundPages;
Expand Down Expand Up @@ -374,7 +371,7 @@ class KeyboardDescriptor: public EventableDescriptor

protected:
bool bReadAttemptedAfterClose;
time_t LastIo;
Int64 LastIo;
int InactivityTimeout;

private:
Expand Down
48 changes: 30 additions & 18 deletions ext/em.cpp
Expand Up @@ -26,7 +26,7 @@ See the file COPYING for complete licensing information.
// Keep a global variable floating around
// with the current loop time as set by the Event Machine.
// This avoids the need for frequent expensive calls to time(NULL);
time_t gCurrentLoopTime;
Int64 gCurrentLoopTime;

#ifdef OS_WIN32
unsigned gTickCountTickover;
Expand Down Expand Up @@ -97,7 +97,7 @@ EventMachine_t::EventMachine_t (void (*event_callback)(const char*, int, const c
gTerminateSignalReceived = false;
// Make sure the current loop time is sane, in case we do any initializations of
// objects before we start running.
gCurrentLoopTime = time(NULL);
_UpdateTime();

/* We initialize the network library here (only on Windows of course)
* and initialize "loop breakers." Our destructor also does some network-level
Expand Down Expand Up @@ -344,6 +344,32 @@ void EventMachine_t::_InitializeLoopBreaker()
#endif
}

/***************************
EventMachine_t::_UpdateTime
***************************/

void EventMachine_t::_UpdateTime()
{
#ifdef OS_UNIX
struct timeval tv;
gettimeofday (&tv, NULL);
gCurrentLoopTime = (((Int64)(tv.tv_sec)) * 1000000LL) + ((Int64)(tv.tv_usec));
#endif

#ifdef OS_WIN32
unsigned tick = GetTickCount();
if (tick < gLastTickCount)
gTickCountTickover += 1;
gLastTickCount = tick;
gCurrentLoopTime = ((Int64)gTickCountTickover << 32) + (Int64)tick;
#endif

#ifndef OS_UNIX
#ifndef OS_WIN32
gCurrentLoopTime = (Int64)time(NULL) * 1000000LL;
#endif
#endif
}

/*******************
EventMachine_t::Run
Expand Down Expand Up @@ -393,7 +419,7 @@ void EventMachine_t::Run()
#endif

while (true) {
gCurrentLoopTime = time(NULL);
_UpdateTime();
if (!_RunTimers())
break;

Expand Down Expand Up @@ -893,25 +919,11 @@ bool EventMachine_t::_RunTimers()
// Just keep inspecting and processing the list head until we hit
// one that hasn't expired yet.

#ifdef OS_UNIX
struct timeval tv;
gettimeofday (&tv, NULL);
Int64 now = (((Int64)(tv.tv_sec)) * 1000000LL) + ((Int64)(tv.tv_usec));
#endif

#ifdef OS_WIN32
unsigned tick = GetTickCount();
if (tick < gLastTickCount)
gTickCountTickover += 1;
gLastTickCount = tick;
Int64 now = ((Int64)gTickCountTickover << 32) + (Int64)tick;
#endif

while (true) {
multimap<Int64,Timer_t>::iterator i = Timers.begin();
if (i == Timers.end())
break;
if (i->first > now)
if (i->first > gCurrentLoopTime)
break;
if (EventCallback)
(*EventCallback) ("", EM_TIMER_FIRED, i->second.GetBinding().c_str(), i->second.GetBinding().length());
Expand Down
5 changes: 3 additions & 2 deletions ext/em.h
Expand Up @@ -57,7 +57,7 @@ typedef long long Int64;
typedef __int64 Int64;
#endif

extern time_t gCurrentLoopTime;
extern Int64 gCurrentLoopTime;

class EventableDescriptor;
class InotifyDescriptor;
Expand Down Expand Up @@ -137,6 +137,7 @@ class EventMachine_t
private:
bool _RunOnce();
bool _RunTimers();
void _UpdateTime();
void _AddNewDescriptors();
void _ModifyDescriptors();
void _InitializeLoopBreaker();
Expand Down Expand Up @@ -169,7 +170,7 @@ class EventMachine_t
vector<EventableDescriptor*> NewDescriptors;
set<EventableDescriptor*> ModifiedDescriptors;

time_t NextHeartbeatTime;
Int64 NextHeartbeatTime;

int LoopBreakerReader;
int LoopBreakerWriter;
Expand Down
4 changes: 2 additions & 2 deletions ext/eventmachine.h
Expand Up @@ -68,8 +68,8 @@ extern "C" {
int evma_get_connection_count();
int evma_send_data_to_connection (const char *binding, const char *data, int data_length);
int evma_send_datagram (const char *binding, const char *data, int data_length, const char *address, int port);
int evma_get_comm_inactivity_timeout (const char *binding, /*out*/int *value);
int evma_set_comm_inactivity_timeout (const char *binding, /*in,out*/int *value);
float evma_get_comm_inactivity_timeout (const char *binding);
int evma_set_comm_inactivity_timeout (const char *binding, float value);
int evma_get_outbound_data_size (const char *binding);
int evma_send_file_data_to_connection (const char *binding, const char *filename);

Expand Down
1 change: 0 additions & 1 deletion ext/pipe.cpp
Expand Up @@ -164,7 +164,6 @@ void PipeDescriptor::Read()

if (r > 0) {
total_bytes_read += r;
LastRead = gCurrentLoopTime;

// Add a null-terminator at the the end of the buffer
// that we will send to the callback.
Expand Down

0 comments on commit 91d5dc3

Please sign in to comment.