Permalink
Browse files

update

  • Loading branch information...
1 parent bec6687 commit c6a9029a0ffc9e73fa1a5a3b65bec14f79a01170 yayanyang committed Sep 30, 2012
Showing with 67 additions and 5 deletions.
  1. +1 −0 io/abi.cpp
  2. +50 −4 io/socket_reactor.cpp
  3. +2 −0 io/unix.cmake
  4. +14 −1 sys/thread.cpp
View
@@ -7,6 +7,7 @@ LEMON_IO_API
LemonCloseIO(
__lemon_free LemonIO io)
{
+ std::cout << "close io" << std::endl;
reinterpret_cast<object*>(io)->release();
}
View
@@ -3,6 +3,8 @@
#ifndef LEMON_IO_IOCP
+#include <poll.h>
+
namespace lemon{namespace io{namespace impl{
inline void __lemon_noblocking_socket(__lemon_native_socket handle, LemonErrorInfo * errorCode)
@@ -14,7 +16,7 @@ namespace lemon{namespace io{namespace impl{
#else
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);
#endif //WIN32
@@ -26,20 +28,31 @@ namespace lemon{namespace io{namespace impl{
LEMON_RESET_ERRORINFO(*errorCode);
+#ifdef WIN32
+
fd_set fds;
FD_ZERO(&fds);
FD_SET(handle, &fds);
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)
{
assert(__lemon_invalid_socket != handle);
LEMON_RESET_ERRORINFO(*errorCode);
+#ifdef WIN32
fd_set fds;
@@ -48,13 +61,22 @@ namespace lemon{namespace io{namespace impl{
FD_SET(handle, &fds);
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)
{
assert(__lemon_invalid_socket != handle);
LEMON_RESET_ERRORINFO(*errorCode);
+#ifdef WIN32
fd_set fds;
@@ -74,7 +96,7 @@ namespace lemon{namespace io{namespace impl{
{
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))
{
@@ -86,7 +108,30 @@ namespace lemon{namespace io{namespace impl{
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
}
}}}
@@ -108,6 +153,7 @@ namespace lemon{namespace io{namespace impl{
void socket_reactor::close(__lemon_native_socket handle)
{
+ std::cout << "call socket close" << std::endl;
closesocket(handle);
}
@@ -170,7 +216,7 @@ namespace lemon{namespace io{namespace impl{
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;
View
@@ -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(sys/eventfd.h LEMON_HAS_EVENTFD_H)
+
+set(LEMON_IO_SELECT TRUE)
View
@@ -558,6 +558,8 @@ LEMON_IMPLEMENT_HANDLE(LemonThread){
pthread_t Handle;
+ lemon_pid_t Id;
+
LemonMutex JoinMutex;
LemonThreadProc Proc;
@@ -583,6 +585,12 @@ void* ProcWrapper(void* 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);
return 0;
@@ -674,7 +682,7 @@ LEMON_SYS_API void LemonThreadJoin(LemonThread t,LemonErrorInfo * errorCode){
LEMON_SYS_API lemon_pid_t LemonGetThreadId(LemonThread t)
{
- return t->Handle;
+ return t->Id;
}
LEMON_SYS_API lemon_pid_t LemonGetCurrentThreadId(LemonErrorInfo * errorCode)
@@ -686,6 +694,11 @@ LEMON_SYS_API lemon_pid_t LemonGetCurrentThreadId(LemonErrorInfo * errorCode)
#endif
}
+LEMON_SYS_API void LemonSleep(size_t milliseconds)
+{
+ ::sleep(milliseconds/1000);
+}
+
#else
# error "not implement"
#endif //LEMON_USE_WIN32_THREAD

0 comments on commit c6a9029

Please sign in to comment.