]> granicus.if.org Git - libevent/commitdiff
make bufferevent_getwatermark api more robust
authorufo2243 <ufo2243@gmail.com>
Fri, 12 Sep 2014 03:51:59 +0000 (11:51 +0800)
committerufo2243 <ufo2243@gmail.com>
Fri, 12 Sep 2014 03:51:59 +0000 (11:51 +0800)
bufferevent.c
include/event2/bufferevent.h
test/regress_bufferevent.c

index 588461a33e1ac63117583ffdd0f8ccfddedbe120..2e6be1444091ae386fd70fc12e9db0b5ee65268b 100644 (file)
@@ -615,25 +615,30 @@ bufferevent_setwatermark(struct bufferevent *bufev, short events,
        BEV_UNLOCK(bufev);
 }
 
-void
+int
 bufferevent_getwatermark(struct bufferevent *bufev, short events,
     size_t *lowmark, size_t *highmark)
 {
-       BEV_LOCK(bufev);
        if (events == EV_WRITE) {
+               BEV_LOCK(bufev);
                if (lowmark)
                        *lowmark = bufev->wm_write.low;
                if (highmark)
                        *highmark = bufev->wm_write.high;
+               BEV_UNLOCK(bufev);
+               return 0;
        }
 
        if (events == EV_READ) {
+               BEV_LOCK(bufev);
                if (lowmark)
                        *lowmark = bufev->wm_read.low;
                if (highmark)
                        *highmark = bufev->wm_read.high;
+               BEV_UNLOCK(bufev);
+               return 0;
        }
-       BEV_UNLOCK(bufev);
+       return -1;
 }
 
 int
index 46203b04f59601961268edd9799e36b11f86f251..a97030ef7b54a25609f73204e23bea3c5d87c368 100644 (file)
@@ -535,8 +535,9 @@ void bufferevent_setwatermark(struct bufferevent *bufev, short events,
     size_t lowmark, size_t highmark);
 
 /**
-  Retrieves the watermarks for read or write events. Result is undefined if
-  events contains both EV_READ and EV_WRITE.
+  Retrieves the watermarks for read or write events.
+  Returns non-zero if events contains not only EV_READ or EV_WRITE.
+  Returns zero if events equal EV_READ or EV_WRITE
 
   @param bufev the bufferevent to be examined
   @param events EV_READ or EV_WRITE
@@ -544,7 +545,7 @@ void bufferevent_setwatermark(struct bufferevent *bufev, short events,
   @param highmark receives the high watermark if not NULL
 */
 EVENT2_EXPORT_SYMBOL
-void bufferevent_getwatermark(struct bufferevent *bufev, short events,
+int bufferevent_getwatermark(struct bufferevent *bufev, short events,
     size_t *lowmark, size_t *highmark);
 
 /**
index 4ec2dccf43e9c1ac32ad62b684050524943b7aad..669efd8ab1f9fb7d9546ca08f5a36d941a2d791d 100644 (file)
@@ -287,6 +287,9 @@ test_bufferevent_watermarks_impl(int use_pair)
        tt_int_op(low, ==, 100);
        tt_int_op(high, ==, 2000);
 
+       int r = bufferevent_getwatermark(bev1, EV_WRITE | EV_READ, &low, &high);
+       tt_int_op(r, !=, 0);
+
        bufferevent_write(bev1, buffer, sizeof(buffer));
 
        event_dispatch();