-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from swoole/master
Sync upstream
- Loading branch information
Showing
25 changed files
with
577 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
use Swoole\Coroutine as co; | ||
|
||
$chan = new co\Channel(2); | ||
$n = 10; | ||
for ($i = 0; $i < $n; $i++) { | ||
go(function () use ($i,$chan) { | ||
$ret = $chan->push($i); | ||
echo "push {$i} res:".var_export($ret, 1)."\n"; | ||
}); | ||
}; | ||
go(function ()use ($chan){ | ||
$bool = true; | ||
while ($bool){ | ||
$data = $chan->pop(); | ||
echo "pop res:".var_export($data, 1)."\n"; | ||
if($data===false){ | ||
$bool = false; | ||
} | ||
//var_dump($data); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
go(function () { | ||
$count = 0; | ||
go(function () use (&$count) { | ||
echo "task 1 start\n"; | ||
co::sleep(0.2); | ||
echo "task 1 resume count $count\n"; | ||
if (++$count === 2) { | ||
co::resume(1); | ||
} | ||
echo "task 1 end\n"; | ||
}); | ||
go(function () use (&$count) { | ||
echo "task 2 start\n"; | ||
co::sleep(0.1); | ||
echo "task 2 resume count $count\n"; | ||
if (++$count === 2) { | ||
co::resume(1); | ||
} | ||
echo "task 2 end\n"; | ||
}); | ||
co::suspend(); | ||
}); | ||
echo "main \n"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#pragma once | ||
|
||
#include "swoole.h" | ||
#include "context.h" | ||
#include "coroutine.h" | ||
#include <string> | ||
#include <iostream> | ||
#include <list> | ||
#include <queue> | ||
#include <sys/stat.h> | ||
|
||
namespace swoole { | ||
|
||
enum channel_op | ||
{ | ||
PRODUCER = 1, | ||
CONSUMER = 2, | ||
}; | ||
|
||
class Channel; | ||
|
||
struct notify_msg_t | ||
{ | ||
Channel *chan; | ||
enum channel_op type; | ||
}; | ||
|
||
struct timeout_msg_t | ||
{ | ||
Channel *chan; | ||
coroutine_t *co; | ||
bool error; | ||
swTimer_node *timer; | ||
}; | ||
|
||
class Channel | ||
{ | ||
private: | ||
std::list<coroutine_t *> producer_queue; | ||
std::list<coroutine_t *> consumer_queue; | ||
std::queue<void *> data_queue; | ||
size_t capacity; | ||
uint32_t notify_producer_count; | ||
uint32_t notify_consumer_count; | ||
|
||
public: | ||
bool closed; | ||
inline bool is_empty() | ||
{ | ||
return data_queue.size() == 0; | ||
} | ||
|
||
inline bool is_full() | ||
{ | ||
return data_queue.size() == capacity; | ||
} | ||
|
||
inline size_t length() | ||
{ | ||
return data_queue.size(); | ||
} | ||
|
||
inline size_t consumer_num() | ||
{ | ||
return consumer_queue.size(); | ||
} | ||
|
||
inline size_t producer_num() | ||
{ | ||
return producer_queue.size(); | ||
} | ||
|
||
inline void remove(coroutine_t *co) | ||
{ | ||
consumer_queue.remove(co); | ||
} | ||
|
||
inline coroutine_t* pop_coroutine(enum channel_op type) | ||
{ | ||
coroutine_t* co; | ||
if (type == PRODUCER) | ||
{ | ||
co = producer_queue.front(); | ||
producer_queue.pop_front(); | ||
notify_producer_count--; | ||
swDebug("resume producer[%d]", coroutine_get_cid(co)); | ||
} | ||
else | ||
{ | ||
co = consumer_queue.front(); | ||
consumer_queue.pop_front(); | ||
notify_consumer_count--; | ||
swDebug("resume consumer[%d]", coroutine_get_cid(co)); | ||
} | ||
return co; | ||
} | ||
|
||
Channel(size_t _capacity); | ||
void yield(enum channel_op type); | ||
void notify(enum channel_op type); | ||
void* pop(double timeout = 0); | ||
bool push(void *data); | ||
bool close(); | ||
}; | ||
|
||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.