Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
yayanyang committed Sep 30, 2012
1 parent bec6687 commit c6a9029
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions io/abi.cpp
Expand Up @@ -7,6 +7,7 @@ LEMON_IO_API
LemonCloseIO( LemonCloseIO(
__lemon_free LemonIO io) __lemon_free LemonIO io)
{ {
std::cout << "close io" << std::endl;
reinterpret_cast<object*>(io)->release(); reinterpret_cast<object*>(io)->release();
} }


Expand Down
54 changes: 50 additions & 4 deletions io/socket_reactor.cpp
Expand Up @@ -3,6 +3,8 @@


#ifndef LEMON_IO_IOCP #ifndef LEMON_IO_IOCP


#include <poll.h>

namespace lemon{namespace io{namespace impl{ namespace lemon{namespace io{namespace impl{


inline void __lemon_noblocking_socket(__lemon_native_socket handle, LemonErrorInfo * errorCode) inline void __lemon_noblocking_socket(__lemon_native_socket handle, LemonErrorInfo * errorCode)
Expand All @@ -14,7 +16,7 @@ namespace lemon{namespace io{namespace impl{
#else #else
int flag = fcntl(handle,F_GETFL,0); int flag = fcntl(handle,F_GETFL,0);


if(-1 == flag) { LEMON_POSIX_ERROR(errorCode,errno); return; } if(-1 == flag) { LEMON_POSIX_ERROR(*errorCode,errno); return; }


if( -1 == fcntl(handle,F_SETFL,flag | O_NONBLOCK) ) LEMON_POSIX_ERROR(*errorCode,errno); if( -1 == fcntl(handle,F_SETFL,flag | O_NONBLOCK) ) LEMON_POSIX_ERROR(*errorCode,errno);
#endif //WIN32 #endif //WIN32
Expand All @@ -26,20 +28,31 @@ namespace lemon{namespace io{namespace impl{


LEMON_RESET_ERRORINFO(*errorCode); LEMON_RESET_ERRORINFO(*errorCode);


#ifdef WIN32

fd_set fds; fd_set fds;


FD_ZERO(&fds); FD_ZERO(&fds);


FD_SET(handle, &fds); FD_SET(handle, &fds);


if(__lemon_socket_error == ::select(1,0,&fds,0,NULL)) LEMON_IO_SOCKET_ERROR(*errorCode); if(__lemon_socket_error == ::select(1,0,&fds,0,NULL)) LEMON_IO_SOCKET_ERROR(*errorCode);
#else
pollfd fds;
fds.fd = handle;
fds.events = POLLOUT;
fds.revents = 0;

if(__lemon_socket_error == ::poll(&fds, 1, -1)) { LEMON_IO_SOCKET_ERROR(*errorCode); }
#endif //
} }


inline void __lemon_poll_read(__lemon_native_socket handle, LemonErrorInfo * errorCode) inline void __lemon_poll_read(__lemon_native_socket handle, LemonErrorInfo * errorCode)
{ {
assert(__lemon_invalid_socket != handle); assert(__lemon_invalid_socket != handle);


LEMON_RESET_ERRORINFO(*errorCode); LEMON_RESET_ERRORINFO(*errorCode);
#ifdef WIN32


fd_set fds; fd_set fds;


Expand All @@ -48,13 +61,22 @@ namespace lemon{namespace io{namespace impl{
FD_SET(handle, &fds); FD_SET(handle, &fds);


if(__lemon_socket_error == ::select(1,&fds,0,0,NULL)) LEMON_IO_SOCKET_ERROR(*errorCode); if(__lemon_socket_error == ::select(1,&fds,0,0,NULL)) LEMON_IO_SOCKET_ERROR(*errorCode);
#else
pollfd fds;
fds.fd = handle;
fds.events = POLLIN;
fds.revents = 0;

if(__lemon_socket_error == ::poll(&fds, 1, -1)) { LEMON_IO_SOCKET_ERROR(*errorCode); }
#endif
} }


inline void __lemon_poll_connect(__lemon_native_socket handle, LemonErrorInfo * errorCode) inline void __lemon_poll_connect(__lemon_native_socket handle, LemonErrorInfo * errorCode)
{ {
assert(__lemon_invalid_socket != handle); assert(__lemon_invalid_socket != handle);


LEMON_RESET_ERRORINFO(*errorCode); LEMON_RESET_ERRORINFO(*errorCode);
#ifdef WIN32


fd_set fds; fd_set fds;


Expand All @@ -74,7 +96,7 @@ namespace lemon{namespace io{namespace impl{
{ {
int connect_error = 0; int connect_error = 0;


int connect_error_len = (int)sizeof(connect_error); socklen_t connect_error_len = (socklen_t)sizeof(connect_error);


if(__lemon_socket_error == ::getsockopt(handle,SOL_SOCKET, SO_ERROR, (char*)&connect_error, &connect_error_len)) if(__lemon_socket_error == ::getsockopt(handle,SOL_SOCKET, SO_ERROR, (char*)&connect_error, &connect_error_len))
{ {
Expand All @@ -86,7 +108,30 @@ namespace lemon{namespace io{namespace impl{


errorCode->Error.Code = connect_error; errorCode->Error.Code = connect_error;
} }
} }
#else
pollfd fds;
fds.fd = handle;
fds.events = POLLOUT;
fds.revents = 0;

if(__lemon_socket_error == ::poll(&fds, 1, -1)) { LEMON_IO_SOCKET_ERROR(*errorCode); return;}

int connect_error = 0;

socklen_t connect_error_len = (socklen_t)sizeof(connect_error);

if(__lemon_socket_error == ::getsockopt(handle,SOL_SOCKET, SO_ERROR, (char*)&connect_error, &connect_error_len))
{
LEMON_IO_SOCKET_ERROR(*errorCode);
}
else if(connect_error)
{
LEMON_IO_SOCKET_ERROR(*errorCode);

errorCode->Error.Code = connect_error;
}
#endif //WIN32
} }
}}} }}}


Expand All @@ -108,6 +153,7 @@ namespace lemon{namespace io{namespace impl{


void socket_reactor::close(__lemon_native_socket handle) void socket_reactor::close(__lemon_native_socket handle)
{ {
std::cout << "call socket close" << std::endl;
closesocket(handle); closesocket(handle);
} }


Expand Down Expand Up @@ -170,7 +216,7 @@ namespace lemon{namespace io{namespace impl{


if(length != __lemon_socket_error) return length; if(length != __lemon_socket_error) return length;


LEMON_IO_SOCKET_ERROR(*errorCode); LEMON_IO_SOCKET_ERROR(* errorCode);


if(errorCode->Error.Code != would_block && errorCode->Error.Code != try_again) return (size_t)-1; if(errorCode->Error.Code != would_block && errorCode->Error.Code != try_again) return (size_t)-1;


Expand Down
2 changes: 2 additions & 0 deletions io/unix.cmake
Expand Up @@ -7,3 +7,5 @@ check_include_files(sys/event.h LEMON_IO_KQUEUE)
check_include_files(fcntl.h LEMON_HAS_FCNTL_H) check_include_files(fcntl.h LEMON_HAS_FCNTL_H)


check_include_files(sys/eventfd.h LEMON_HAS_EVENTFD_H) check_include_files(sys/eventfd.h LEMON_HAS_EVENTFD_H)

set(LEMON_IO_SELECT TRUE)
15 changes: 14 additions & 1 deletion sys/thread.cpp
Expand Up @@ -558,6 +558,8 @@ LEMON_IMPLEMENT_HANDLE(LemonThread){


pthread_t Handle; pthread_t Handle;


lemon_pid_t Id;

LemonMutex JoinMutex; LemonMutex JoinMutex;


LemonThreadProc Proc; LemonThreadProc Proc;
Expand All @@ -583,6 +585,12 @@ void* ProcWrapper(void* data)


LemonThread current = (LemonThread)data; LemonThread current = (LemonThread)data;


#ifdef LEMON_HAS_GETTID
current->Id = gettid();
#else
current->Id = (pid_t) syscall (SYS_gettid);
#endif

current->Proc(current->UserData); current->Proc(current->UserData);


return 0; return 0;
Expand Down Expand Up @@ -674,7 +682,7 @@ LEMON_SYS_API void LemonThreadJoin(LemonThread t,LemonErrorInfo * errorCode){


LEMON_SYS_API lemon_pid_t LemonGetThreadId(LemonThread t) LEMON_SYS_API lemon_pid_t LemonGetThreadId(LemonThread t)
{ {
return t->Handle; return t->Id;
} }


LEMON_SYS_API lemon_pid_t LemonGetCurrentThreadId(LemonErrorInfo * errorCode) LEMON_SYS_API lemon_pid_t LemonGetCurrentThreadId(LemonErrorInfo * errorCode)
Expand All @@ -686,6 +694,11 @@ LEMON_SYS_API lemon_pid_t LemonGetCurrentThreadId(LemonErrorInfo * errorCode)
#endif #endif
} }


LEMON_SYS_API void LemonSleep(size_t milliseconds)
{
::sleep(milliseconds/1000);
}

#else #else
# error "not implement" # error "not implement"
#endif //LEMON_USE_WIN32_THREAD #endif //LEMON_USE_WIN32_THREAD
Expand Down

0 comments on commit c6a9029

Please sign in to comment.