redis-server 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (290) hide show
  1. data/LICENSE +38 -0
  2. data/README.md +33 -0
  3. data/bin/redis +114 -0
  4. data/redis/Makefile +5 -0
  5. data/redis/extconf.rb +3 -0
  6. data/redis/redis-2.2.11/00-RELEASENOTES +199 -0
  7. data/redis/redis-2.2.11/BUGS +1 -0
  8. data/redis/redis-2.2.11/CONTRIBUTING +13 -0
  9. data/redis/redis-2.2.11/COPYING +10 -0
  10. data/redis/redis-2.2.11/Changelog +1032 -0
  11. data/redis/redis-2.2.11/INSTALL +30 -0
  12. data/redis/redis-2.2.11/Makefile +22 -0
  13. data/redis/redis-2.2.11/README +83 -0
  14. data/redis/redis-2.2.11/TODO +4 -0
  15. data/redis/redis-2.2.11/client-libraries/README +11 -0
  16. data/redis/redis-2.2.11/deps/hiredis/COPYING +10 -0
  17. data/redis/redis-2.2.11/deps/hiredis/Makefile +115 -0
  18. data/redis/redis-2.2.11/deps/hiredis/README.md +311 -0
  19. data/redis/redis-2.2.11/deps/hiredis/TODO +2 -0
  20. data/redis/redis-2.2.11/deps/hiredis/adapters/ae.h +95 -0
  21. data/redis/redis-2.2.11/deps/hiredis/adapters/libev.h +113 -0
  22. data/redis/redis-2.2.11/deps/hiredis/adapters/libevent.h +76 -0
  23. data/redis/redis-2.2.11/deps/hiredis/async.c +321 -0
  24. data/redis/redis-2.2.11/deps/hiredis/async.h +112 -0
  25. data/redis/redis-2.2.11/deps/hiredis/example-ae.c +53 -0
  26. data/redis/redis-2.2.11/deps/hiredis/example-libev.c +47 -0
  27. data/redis/redis-2.2.11/deps/hiredis/example-libevent.c +48 -0
  28. data/redis/redis-2.2.11/deps/hiredis/example.c +67 -0
  29. data/redis/redis-2.2.11/deps/hiredis/fmacros.h +15 -0
  30. data/redis/redis-2.2.11/deps/hiredis/hiredis.c +1058 -0
  31. data/redis/redis-2.2.11/deps/hiredis/hiredis.h +170 -0
  32. data/redis/redis-2.2.11/deps/hiredis/net.c +170 -0
  33. data/redis/redis-2.2.11/deps/hiredis/net.h +43 -0
  34. data/redis/redis-2.2.11/deps/hiredis/sds.c +479 -0
  35. data/redis/redis-2.2.11/deps/hiredis/sds.h +77 -0
  36. data/redis/redis-2.2.11/deps/hiredis/test.c +479 -0
  37. data/redis/redis-2.2.11/deps/hiredis/util.h +40 -0
  38. data/redis/redis-2.2.11/deps/linenoise/Makefile +10 -0
  39. data/redis/redis-2.2.11/deps/linenoise/README.markdown +45 -0
  40. data/redis/redis-2.2.11/deps/linenoise/example.c +27 -0
  41. data/redis/redis-2.2.11/deps/linenoise/linenoise.c +609 -0
  42. data/redis/redis-2.2.11/deps/linenoise/linenoise.h +55 -0
  43. data/redis/redis-2.2.11/design-documents/REDIS-CLUSTER +214 -0
  44. data/redis/redis-2.2.11/design-documents/REDIS-CLUSTER-2 +343 -0
  45. data/redis/redis-2.2.11/doc/AppendCommand.html +48 -0
  46. data/redis/redis-2.2.11/doc/AppendOnlyFileHowto.html +41 -0
  47. data/redis/redis-2.2.11/doc/AuthCommand.html +39 -0
  48. data/redis/redis-2.2.11/doc/Benchmarks.html +129 -0
  49. data/redis/redis-2.2.11/doc/BgrewriteaofCommand.html +41 -0
  50. data/redis/redis-2.2.11/doc/BgsaveCommand.html +39 -0
  51. data/redis/redis-2.2.11/doc/BlpopCommand.html +51 -0
  52. data/redis/redis-2.2.11/doc/BrpoplpushCommand.html +39 -0
  53. data/redis/redis-2.2.11/doc/CommandReference.html +47 -0
  54. data/redis/redis-2.2.11/doc/Comparisons.html +42 -0
  55. data/redis/redis-2.2.11/doc/ConfigCommand.html +76 -0
  56. data/redis/redis-2.2.11/doc/Configuration.html +38 -0
  57. data/redis/redis-2.2.11/doc/ConnectionHandlingSidebar.html +36 -0
  58. data/redis/redis-2.2.11/doc/ControlCommandsSidebar.html +36 -0
  59. data/redis/redis-2.2.11/doc/Credits.html +38 -0
  60. data/redis/redis-2.2.11/doc/DbsizeCommand.html +38 -0
  61. data/redis/redis-2.2.11/doc/DelCommand.html +41 -0
  62. data/redis/redis-2.2.11/doc/DesignPatterns.html +37 -0
  63. data/redis/redis-2.2.11/doc/EventLibray.html +44 -0
  64. data/redis/redis-2.2.11/doc/ExistsCommand.html +42 -0
  65. data/redis/redis-2.2.11/doc/ExpireCommand.html +96 -0
  66. data/redis/redis-2.2.11/doc/FAQ.html +70 -0
  67. data/redis/redis-2.2.11/doc/Features.html +38 -0
  68. data/redis/redis-2.2.11/doc/FlushallCommand.html +39 -0
  69. data/redis/redis-2.2.11/doc/FlushdbCommand.html +39 -0
  70. data/redis/redis-2.2.11/doc/FromSqlToDataStructures.html +37 -0
  71. data/redis/redis-2.2.11/doc/GenericCommandsSidebar.html +36 -0
  72. data/redis/redis-2.2.11/doc/GetCommand.html +39 -0
  73. data/redis/redis-2.2.11/doc/GetbitCommand.html +39 -0
  74. data/redis/redis-2.2.11/doc/GetsetCommand.html +38 -0
  75. data/redis/redis-2.2.11/doc/HackingStrings.html +83 -0
  76. data/redis/redis-2.2.11/doc/HashCommandsSidebar.html +36 -0
  77. data/redis/redis-2.2.11/doc/Hashes.html +37 -0
  78. data/redis/redis-2.2.11/doc/HdelCommand.html +39 -0
  79. data/redis/redis-2.2.11/doc/HexistsCommand.html +39 -0
  80. data/redis/redis-2.2.11/doc/HgetCommand.html +39 -0
  81. data/redis/redis-2.2.11/doc/HgetallCommand.html +40 -0
  82. data/redis/redis-2.2.11/doc/HincrbyCommand.html +45 -0
  83. data/redis/redis-2.2.11/doc/HlenCommand.html +38 -0
  84. data/redis/redis-2.2.11/doc/HmgetCommand.html +40 -0
  85. data/redis/redis-2.2.11/doc/HmsetCommand.html +40 -0
  86. data/redis/redis-2.2.11/doc/HsetCommand.html +40 -0
  87. data/redis/redis-2.2.11/doc/HsetnxCommand.html +41 -0
  88. data/redis/redis-2.2.11/doc/IncrCommand.html +43 -0
  89. data/redis/redis-2.2.11/doc/InfoCommand.html +48 -0
  90. data/redis/redis-2.2.11/doc/IntroductionToRedisDataTypes.html +152 -0
  91. data/redis/redis-2.2.11/doc/KeysCommand.html +43 -0
  92. data/redis/redis-2.2.11/doc/LastsaveCommand.html +39 -0
  93. data/redis/redis-2.2.11/doc/LindexCommand.html +40 -0
  94. data/redis/redis-2.2.11/doc/ListCommandsSidebar.html +36 -0
  95. data/redis/redis-2.2.11/doc/Lists.html +42 -0
  96. data/redis/redis-2.2.11/doc/LlenCommand.html +41 -0
  97. data/redis/redis-2.2.11/doc/LpopCommand.html +41 -0
  98. data/redis/redis-2.2.11/doc/LrangeCommand.html +47 -0
  99. data/redis/redis-2.2.11/doc/LremCommand.html +41 -0
  100. data/redis/redis-2.2.11/doc/LsetCommand.html +38 -0
  101. data/redis/redis-2.2.11/doc/LtrimCommand.html +47 -0
  102. data/redis/redis-2.2.11/doc/MgetCommand.html +52 -0
  103. data/redis/redis-2.2.11/doc/MonitorCommand.html +63 -0
  104. data/redis/redis-2.2.11/doc/MoveCommand.html +42 -0
  105. data/redis/redis-2.2.11/doc/MsetCommand.html +44 -0
  106. data/redis/redis-2.2.11/doc/MultiExecCommand.html +166 -0
  107. data/redis/redis-2.2.11/doc/NonexistentCommands.html +51 -0
  108. data/redis/redis-2.2.11/doc/ObjectHashMappers.html +39 -0
  109. data/redis/redis-2.2.11/doc/Pipelining.html +36 -0
  110. data/redis/redis-2.2.11/doc/ProgrammingExamples.html +38 -0
  111. data/redis/redis-2.2.11/doc/ProtocolSpecification.html +137 -0
  112. data/redis/redis-2.2.11/doc/PublishSubscribe.html +115 -0
  113. data/redis/redis-2.2.11/doc/QuickStart.html +68 -0
  114. data/redis/redis-2.2.11/doc/QuitCommand.html +38 -0
  115. data/redis/redis-2.2.11/doc/README.html +119 -0
  116. data/redis/redis-2.2.11/doc/RandomkeyCommand.html +39 -0
  117. data/redis/redis-2.2.11/doc/Redis0100ChangeLog.html +67 -0
  118. data/redis/redis-2.2.11/doc/Redis0900ChangeLog.html +56 -0
  119. data/redis/redis-2.2.11/doc/RedisBigData.html +61 -0
  120. data/redis/redis-2.2.11/doc/RedisCLI.html +37 -0
  121. data/redis/redis-2.2.11/doc/RedisEventLibrary.html +70 -0
  122. data/redis/redis-2.2.11/doc/RedisGuides.html +37 -0
  123. data/redis/redis-2.2.11/doc/RedisInternals.html +38 -0
  124. data/redis/redis-2.2.11/doc/RedisPipelining.html +93 -0
  125. data/redis/redis-2.2.11/doc/RedisStatus.html +56 -0
  126. data/redis/redis-2.2.11/doc/Redis_1_2_0_Changelog.html +40 -0
  127. data/redis/redis-2.2.11/doc/Redis_2_0_0_Changelog.html +62 -0
  128. data/redis/redis-2.2.11/doc/Redis_2_0_Whats_new.html +59 -0
  129. data/redis/redis-2.2.11/doc/RenameCommand.html +39 -0
  130. data/redis/redis-2.2.11/doc/RenamenxCommand.html +42 -0
  131. data/redis/redis-2.2.11/doc/ReplicationHowto.html +43 -0
  132. data/redis/redis-2.2.11/doc/ReplyTypes.html +42 -0
  133. data/redis/redis-2.2.11/doc/RoadMap.html +38 -0
  134. data/redis/redis-2.2.11/doc/RpoplpushCommand.html +44 -0
  135. data/redis/redis-2.2.11/doc/RpushCommand.html +40 -0
  136. data/redis/redis-2.2.11/doc/SaddCommand.html +41 -0
  137. data/redis/redis-2.2.11/doc/SaveCommand.html +39 -0
  138. data/redis/redis-2.2.11/doc/ScardCommand.html +41 -0
  139. data/redis/redis-2.2.11/doc/SdiffCommand.html +45 -0
  140. data/redis/redis-2.2.11/doc/SdiffstoreCommand.html +38 -0
  141. data/redis/redis-2.2.11/doc/SelectCommand.html +39 -0
  142. data/redis/redis-2.2.11/doc/SetCommand.html +39 -0
  143. data/redis/redis-2.2.11/doc/SetCommandsSidebar.html +36 -0
  144. data/redis/redis-2.2.11/doc/SetbitCommand.html +45 -0
  145. data/redis/redis-2.2.11/doc/SetexCommand.html +42 -0
  146. data/redis/redis-2.2.11/doc/SetnxCommand.html +51 -0
  147. data/redis/redis-2.2.11/doc/SetrangeCommand.html +58 -0
  148. data/redis/redis-2.2.11/doc/Sets.html +36 -0
  149. data/redis/redis-2.2.11/doc/ShutdownCommand.html +39 -0
  150. data/redis/redis-2.2.11/doc/SideBar.html +36 -0
  151. data/redis/redis-2.2.11/doc/SinterCommand.html +40 -0
  152. data/redis/redis-2.2.11/doc/SinterstoreCommand.html +39 -0
  153. data/redis/redis-2.2.11/doc/SismemberCommand.html +42 -0
  154. data/redis/redis-2.2.11/doc/SlaveofCommand.html +41 -0
  155. data/redis/redis-2.2.11/doc/SmembersCommand.html +38 -0
  156. data/redis/redis-2.2.11/doc/SmoveCommand.html +44 -0
  157. data/redis/redis-2.2.11/doc/SortCommand.html +75 -0
  158. data/redis/redis-2.2.11/doc/SortedSetCommandsSidebar.html +36 -0
  159. data/redis/redis-2.2.11/doc/SortedSets.html +36 -0
  160. data/redis/redis-2.2.11/doc/Speed.html +38 -0
  161. data/redis/redis-2.2.11/doc/SponsorshipHistory.html +38 -0
  162. data/redis/redis-2.2.11/doc/SpopCommand.html +40 -0
  163. data/redis/redis-2.2.11/doc/SrandmemberCommand.html +40 -0
  164. data/redis/redis-2.2.11/doc/SremCommand.html +42 -0
  165. data/redis/redis-2.2.11/doc/StringCommandsSidebar.html +36 -0
  166. data/redis/redis-2.2.11/doc/Strings.html +37 -0
  167. data/redis/redis-2.2.11/doc/StrlenCommand.html +39 -0
  168. data/redis/redis-2.2.11/doc/SubstrCommand.html +52 -0
  169. data/redis/redis-2.2.11/doc/SunionCommand.html +40 -0
  170. data/redis/redis-2.2.11/doc/SunionstoreCommand.html +38 -0
  171. data/redis/redis-2.2.11/doc/SupportedLanguages.html +60 -0
  172. data/redis/redis-2.2.11/doc/SupportedPlatforms.html +37 -0
  173. data/redis/redis-2.2.11/doc/TemplateCommand.html +38 -0
  174. data/redis/redis-2.2.11/doc/TtlCommand.html +38 -0
  175. data/redis/redis-2.2.11/doc/TwitterAlikeExample.html +250 -0
  176. data/redis/redis-2.2.11/doc/TypeCommand.html +46 -0
  177. data/redis/redis-2.2.11/doc/UnstableSource.html +39 -0
  178. data/redis/redis-2.2.11/doc/VirtualMemorySpecification.html +156 -0
  179. data/redis/redis-2.2.11/doc/VirtualMemoryUserGuide.html +66 -0
  180. data/redis/redis-2.2.11/doc/ZaddCommand.html +43 -0
  181. data/redis/redis-2.2.11/doc/ZcardCommand.html +41 -0
  182. data/redis/redis-2.2.11/doc/ZincrbyCommand.html +42 -0
  183. data/redis/redis-2.2.11/doc/ZrangeCommand.html +42 -0
  184. data/redis/redis-2.2.11/doc/ZrangebyscoreCommand.html +77 -0
  185. data/redis/redis-2.2.11/doc/ZrankCommand.html +43 -0
  186. data/redis/redis-2.2.11/doc/ZremCommand.html +42 -0
  187. data/redis/redis-2.2.11/doc/ZremrangebyrankCommand.html +39 -0
  188. data/redis/redis-2.2.11/doc/ZremrangebyscoreCommand.html +39 -0
  189. data/redis/redis-2.2.11/doc/ZscoreCommand.html +41 -0
  190. data/redis/redis-2.2.11/doc/ZunionCommand.html +42 -0
  191. data/redis/redis-2.2.11/doc/ZunionstoreCommand.html +43 -0
  192. data/redis/redis-2.2.11/doc/index.html +43 -0
  193. data/redis/redis-2.2.11/doc/redis.png +0 -0
  194. data/redis/redis-2.2.11/doc/style.css +25 -0
  195. data/redis/redis-2.2.11/redis.conf +417 -0
  196. data/redis/redis-2.2.11/src/Makefile +177 -0
  197. data/redis/redis-2.2.11/src/adlist.c +325 -0
  198. data/redis/redis-2.2.11/src/adlist.h +92 -0
  199. data/redis/redis-2.2.11/src/ae.c +390 -0
  200. data/redis/redis-2.2.11/src/ae.h +117 -0
  201. data/redis/redis-2.2.11/src/ae_epoll.c +91 -0
  202. data/redis/redis-2.2.11/src/ae_kqueue.c +93 -0
  203. data/redis/redis-2.2.11/src/ae_select.c +72 -0
  204. data/redis/redis-2.2.11/src/anet.c +347 -0
  205. data/redis/redis-2.2.11/src/anet.h +57 -0
  206. data/redis/redis-2.2.11/src/aof.c +675 -0
  207. data/redis/redis-2.2.11/src/config.c +627 -0
  208. data/redis/redis-2.2.11/src/config.h +64 -0
  209. data/redis/redis-2.2.11/src/db.c +543 -0
  210. data/redis/redis-2.2.11/src/debug.c +314 -0
  211. data/redis/redis-2.2.11/src/dict.c +721 -0
  212. data/redis/redis-2.2.11/src/dict.h +156 -0
  213. data/redis/redis-2.2.11/src/fmacros.h +15 -0
  214. data/redis/redis-2.2.11/src/help.h +638 -0
  215. data/redis/redis-2.2.11/src/intset.c +422 -0
  216. data/redis/redis-2.2.11/src/intset.h +19 -0
  217. data/redis/redis-2.2.11/src/lzf.h +100 -0
  218. data/redis/redis-2.2.11/src/lzfP.h +159 -0
  219. data/redis/redis-2.2.11/src/lzf_c.c +295 -0
  220. data/redis/redis-2.2.11/src/lzf_d.c +150 -0
  221. data/redis/redis-2.2.11/src/mkreleasehdr.sh +9 -0
  222. data/redis/redis-2.2.11/src/multi.c +268 -0
  223. data/redis/redis-2.2.11/src/networking.c +899 -0
  224. data/redis/redis-2.2.11/src/object.c +484 -0
  225. data/redis/redis-2.2.11/src/pqsort.c +197 -0
  226. data/redis/redis-2.2.11/src/pqsort.h +15 -0
  227. data/redis/redis-2.2.11/src/pubsub.c +267 -0
  228. data/redis/redis-2.2.11/src/rdb.c +1020 -0
  229. data/redis/redis-2.2.11/src/redis-benchmark.c +530 -0
  230. data/redis/redis-2.2.11/src/redis-check-aof.c +185 -0
  231. data/redis/redis-2.2.11/src/redis-check-dump.c +681 -0
  232. data/redis/redis-2.2.11/src/redis-cli.c +773 -0
  233. data/redis/redis-2.2.11/src/redis.c +1677 -0
  234. data/redis/redis-2.2.11/src/redis.h +1022 -0
  235. data/redis/redis-2.2.11/src/release.c +13 -0
  236. data/redis/redis-2.2.11/src/replication.c +557 -0
  237. data/redis/redis-2.2.11/src/sds.c +639 -0
  238. data/redis/redis-2.2.11/src/sds.h +78 -0
  239. data/redis/redis-2.2.11/src/sha1.c +276 -0
  240. data/redis/redis-2.2.11/src/sha1.h +17 -0
  241. data/redis/redis-2.2.11/src/solarisfixes.h +22 -0
  242. data/redis/redis-2.2.11/src/sort.c +389 -0
  243. data/redis/redis-2.2.11/src/syncio.c +154 -0
  244. data/redis/redis-2.2.11/src/t_hash.c +476 -0
  245. data/redis/redis-2.2.11/src/t_list.c +986 -0
  246. data/redis/redis-2.2.11/src/t_set.c +610 -0
  247. data/redis/redis-2.2.11/src/t_string.c +438 -0
  248. data/redis/redis-2.2.11/src/t_zset.c +1084 -0
  249. data/redis/redis-2.2.11/src/testhelp.h +54 -0
  250. data/redis/redis-2.2.11/src/util.c +243 -0
  251. data/redis/redis-2.2.11/src/valgrind.sup +5 -0
  252. data/redis/redis-2.2.11/src/version.h +1 -0
  253. data/redis/redis-2.2.11/src/vm.c +1149 -0
  254. data/redis/redis-2.2.11/src/ziplist.c +1323 -0
  255. data/redis/redis-2.2.11/src/ziplist.h +15 -0
  256. data/redis/redis-2.2.11/src/zipmap.c +455 -0
  257. data/redis/redis-2.2.11/src/zipmap.h +48 -0
  258. data/redis/redis-2.2.11/src/zmalloc.c +278 -0
  259. data/redis/redis-2.2.11/src/zmalloc.h +47 -0
  260. data/redis/redis-2.2.11/tests/assets/default.conf +308 -0
  261. data/redis/redis-2.2.11/tests/integration/aof.tcl +104 -0
  262. data/redis/redis-2.2.11/tests/integration/redis-cli.tcl +208 -0
  263. data/redis/redis-2.2.11/tests/integration/replication.tcl +98 -0
  264. data/redis/redis-2.2.11/tests/support/redis.tcl +241 -0
  265. data/redis/redis-2.2.11/tests/support/server.tcl +294 -0
  266. data/redis/redis-2.2.11/tests/support/test.tcl +190 -0
  267. data/redis/redis-2.2.11/tests/support/tmpfile.tcl +15 -0
  268. data/redis/redis-2.2.11/tests/support/util.tcl +296 -0
  269. data/redis/redis-2.2.11/tests/test_helper.tcl +221 -0
  270. data/redis/redis-2.2.11/tests/unit/auth.tcl +15 -0
  271. data/redis/redis-2.2.11/tests/unit/basic.tcl +616 -0
  272. data/redis/redis-2.2.11/tests/unit/cas.tcl +135 -0
  273. data/redis/redis-2.2.11/tests/unit/expire.tcl +74 -0
  274. data/redis/redis-2.2.11/tests/unit/other.tcl +240 -0
  275. data/redis/redis-2.2.11/tests/unit/printver.tcl +6 -0
  276. data/redis/redis-2.2.11/tests/unit/protocol.tcl +62 -0
  277. data/redis/redis-2.2.11/tests/unit/pubsub.tcl +195 -0
  278. data/redis/redis-2.2.11/tests/unit/quit.tcl +40 -0
  279. data/redis/redis-2.2.11/tests/unit/sort.tcl +189 -0
  280. data/redis/redis-2.2.11/tests/unit/type/hash.tcl +300 -0
  281. data/redis/redis-2.2.11/tests/unit/type/list.tcl +819 -0
  282. data/redis/redis-2.2.11/tests/unit/type/set.tcl +334 -0
  283. data/redis/redis-2.2.11/tests/unit/type/zset.tcl +587 -0
  284. data/redis/redis-2.2.11/utils/build-static-symbols.tcl +22 -0
  285. data/redis/redis-2.2.11/utils/generate-command-help.rb +112 -0
  286. data/redis/redis-2.2.11/utils/mktarball.sh +13 -0
  287. data/redis/redis-2.2.11/utils/redis-copy.rb +78 -0
  288. data/redis/redis-2.2.11/utils/redis-sha1.rb +52 -0
  289. data/redis/redis-2.2.11/utils/redis_init_script +42 -0
  290. metadata +362 -0
@@ -0,0 +1,325 @@
1
+ /* adlist.c - A generic doubly linked list implementation
2
+ *
3
+ * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice,
10
+ * this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the name of Redis nor the names of its contributors may be used
15
+ * to endorse or promote products derived from this software without
16
+ * specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+
32
+ #include <stdlib.h>
33
+ #include "adlist.h"
34
+ #include "zmalloc.h"
35
+
36
+ /* Create a new list. The created list can be freed with
37
+ * AlFreeList(), but private value of every node need to be freed
38
+ * by the user before to call AlFreeList().
39
+ *
40
+ * On error, NULL is returned. Otherwise the pointer to the new list. */
41
+ list *listCreate(void)
42
+ {
43
+ struct list *list;
44
+
45
+ if ((list = zmalloc(sizeof(*list))) == NULL)
46
+ return NULL;
47
+ list->head = list->tail = NULL;
48
+ list->len = 0;
49
+ list->dup = NULL;
50
+ list->free = NULL;
51
+ list->match = NULL;
52
+ return list;
53
+ }
54
+
55
+ /* Free the whole list.
56
+ *
57
+ * This function can't fail. */
58
+ void listRelease(list *list)
59
+ {
60
+ unsigned int len;
61
+ listNode *current, *next;
62
+
63
+ current = list->head;
64
+ len = list->len;
65
+ while(len--) {
66
+ next = current->next;
67
+ if (list->free) list->free(current->value);
68
+ zfree(current);
69
+ current = next;
70
+ }
71
+ zfree(list);
72
+ }
73
+
74
+ /* Add a new node to the list, to head, contaning the specified 'value'
75
+ * pointer as value.
76
+ *
77
+ * On error, NULL is returned and no operation is performed (i.e. the
78
+ * list remains unaltered).
79
+ * On success the 'list' pointer you pass to the function is returned. */
80
+ list *listAddNodeHead(list *list, void *value)
81
+ {
82
+ listNode *node;
83
+
84
+ if ((node = zmalloc(sizeof(*node))) == NULL)
85
+ return NULL;
86
+ node->value = value;
87
+ if (list->len == 0) {
88
+ list->head = list->tail = node;
89
+ node->prev = node->next = NULL;
90
+ } else {
91
+ node->prev = NULL;
92
+ node->next = list->head;
93
+ list->head->prev = node;
94
+ list->head = node;
95
+ }
96
+ list->len++;
97
+ return list;
98
+ }
99
+
100
+ /* Add a new node to the list, to tail, contaning the specified 'value'
101
+ * pointer as value.
102
+ *
103
+ * On error, NULL is returned and no operation is performed (i.e. the
104
+ * list remains unaltered).
105
+ * On success the 'list' pointer you pass to the function is returned. */
106
+ list *listAddNodeTail(list *list, void *value)
107
+ {
108
+ listNode *node;
109
+
110
+ if ((node = zmalloc(sizeof(*node))) == NULL)
111
+ return NULL;
112
+ node->value = value;
113
+ if (list->len == 0) {
114
+ list->head = list->tail = node;
115
+ node->prev = node->next = NULL;
116
+ } else {
117
+ node->prev = list->tail;
118
+ node->next = NULL;
119
+ list->tail->next = node;
120
+ list->tail = node;
121
+ }
122
+ list->len++;
123
+ return list;
124
+ }
125
+
126
+ list *listInsertNode(list *list, listNode *old_node, void *value, int after) {
127
+ listNode *node;
128
+
129
+ if ((node = zmalloc(sizeof(*node))) == NULL)
130
+ return NULL;
131
+ node->value = value;
132
+ if (after) {
133
+ node->prev = old_node;
134
+ node->next = old_node->next;
135
+ if (list->tail == old_node) {
136
+ list->tail = node;
137
+ }
138
+ } else {
139
+ node->next = old_node;
140
+ node->prev = old_node->prev;
141
+ if (list->head == old_node) {
142
+ list->head = node;
143
+ }
144
+ }
145
+ if (node->prev != NULL) {
146
+ node->prev->next = node;
147
+ }
148
+ if (node->next != NULL) {
149
+ node->next->prev = node;
150
+ }
151
+ list->len++;
152
+ return list;
153
+ }
154
+
155
+ /* Remove the specified node from the specified list.
156
+ * It's up to the caller to free the private value of the node.
157
+ *
158
+ * This function can't fail. */
159
+ void listDelNode(list *list, listNode *node)
160
+ {
161
+ if (node->prev)
162
+ node->prev->next = node->next;
163
+ else
164
+ list->head = node->next;
165
+ if (node->next)
166
+ node->next->prev = node->prev;
167
+ else
168
+ list->tail = node->prev;
169
+ if (list->free) list->free(node->value);
170
+ zfree(node);
171
+ list->len--;
172
+ }
173
+
174
+ /* Returns a list iterator 'iter'. After the initialization every
175
+ * call to listNext() will return the next element of the list.
176
+ *
177
+ * This function can't fail. */
178
+ listIter *listGetIterator(list *list, int direction)
179
+ {
180
+ listIter *iter;
181
+
182
+ if ((iter = zmalloc(sizeof(*iter))) == NULL) return NULL;
183
+ if (direction == AL_START_HEAD)
184
+ iter->next = list->head;
185
+ else
186
+ iter->next = list->tail;
187
+ iter->direction = direction;
188
+ return iter;
189
+ }
190
+
191
+ /* Release the iterator memory */
192
+ void listReleaseIterator(listIter *iter) {
193
+ zfree(iter);
194
+ }
195
+
196
+ /* Create an iterator in the list private iterator structure */
197
+ void listRewind(list *list, listIter *li) {
198
+ li->next = list->head;
199
+ li->direction = AL_START_HEAD;
200
+ }
201
+
202
+ void listRewindTail(list *list, listIter *li) {
203
+ li->next = list->tail;
204
+ li->direction = AL_START_TAIL;
205
+ }
206
+
207
+ /* Return the next element of an iterator.
208
+ * It's valid to remove the currently returned element using
209
+ * listDelNode(), but not to remove other elements.
210
+ *
211
+ * The function returns a pointer to the next element of the list,
212
+ * or NULL if there are no more elements, so the classical usage patter
213
+ * is:
214
+ *
215
+ * iter = listGetIterator(list,<direction>);
216
+ * while ((node = listNext(iter)) != NULL) {
217
+ * doSomethingWith(listNodeValue(node));
218
+ * }
219
+ *
220
+ * */
221
+ listNode *listNext(listIter *iter)
222
+ {
223
+ listNode *current = iter->next;
224
+
225
+ if (current != NULL) {
226
+ if (iter->direction == AL_START_HEAD)
227
+ iter->next = current->next;
228
+ else
229
+ iter->next = current->prev;
230
+ }
231
+ return current;
232
+ }
233
+
234
+ /* Duplicate the whole list. On out of memory NULL is returned.
235
+ * On success a copy of the original list is returned.
236
+ *
237
+ * The 'Dup' method set with listSetDupMethod() function is used
238
+ * to copy the node value. Otherwise the same pointer value of
239
+ * the original node is used as value of the copied node.
240
+ *
241
+ * The original list both on success or error is never modified. */
242
+ list *listDup(list *orig)
243
+ {
244
+ list *copy;
245
+ listIter *iter;
246
+ listNode *node;
247
+
248
+ if ((copy = listCreate()) == NULL)
249
+ return NULL;
250
+ copy->dup = orig->dup;
251
+ copy->free = orig->free;
252
+ copy->match = orig->match;
253
+ iter = listGetIterator(orig, AL_START_HEAD);
254
+ while((node = listNext(iter)) != NULL) {
255
+ void *value;
256
+
257
+ if (copy->dup) {
258
+ value = copy->dup(node->value);
259
+ if (value == NULL) {
260
+ listRelease(copy);
261
+ listReleaseIterator(iter);
262
+ return NULL;
263
+ }
264
+ } else
265
+ value = node->value;
266
+ if (listAddNodeTail(copy, value) == NULL) {
267
+ listRelease(copy);
268
+ listReleaseIterator(iter);
269
+ return NULL;
270
+ }
271
+ }
272
+ listReleaseIterator(iter);
273
+ return copy;
274
+ }
275
+
276
+ /* Search the list for a node matching a given key.
277
+ * The match is performed using the 'match' method
278
+ * set with listSetMatchMethod(). If no 'match' method
279
+ * is set, the 'value' pointer of every node is directly
280
+ * compared with the 'key' pointer.
281
+ *
282
+ * On success the first matching node pointer is returned
283
+ * (search starts from head). If no matching node exists
284
+ * NULL is returned. */
285
+ listNode *listSearchKey(list *list, void *key)
286
+ {
287
+ listIter *iter;
288
+ listNode *node;
289
+
290
+ iter = listGetIterator(list, AL_START_HEAD);
291
+ while((node = listNext(iter)) != NULL) {
292
+ if (list->match) {
293
+ if (list->match(node->value, key)) {
294
+ listReleaseIterator(iter);
295
+ return node;
296
+ }
297
+ } else {
298
+ if (key == node->value) {
299
+ listReleaseIterator(iter);
300
+ return node;
301
+ }
302
+ }
303
+ }
304
+ listReleaseIterator(iter);
305
+ return NULL;
306
+ }
307
+
308
+ /* Return the element at the specified zero-based index
309
+ * where 0 is the head, 1 is the element next to head
310
+ * and so on. Negative integers are used in order to count
311
+ * from the tail, -1 is the last element, -2 the penultimante
312
+ * and so on. If the index is out of range NULL is returned. */
313
+ listNode *listIndex(list *list, int index) {
314
+ listNode *n;
315
+
316
+ if (index < 0) {
317
+ index = (-index)-1;
318
+ n = list->tail;
319
+ while(index-- && n) n = n->prev;
320
+ } else {
321
+ n = list->head;
322
+ while(index-- && n) n = n->next;
323
+ }
324
+ return n;
325
+ }
@@ -0,0 +1,92 @@
1
+ /* adlist.h - A generic doubly linked list implementation
2
+ *
3
+ * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
4
+ * All rights reserved.
5
+ *
6
+ * Redistribution and use in source and binary forms, with or without
7
+ * modification, are permitted provided that the following conditions are met:
8
+ *
9
+ * * Redistributions of source code must retain the above copyright notice,
10
+ * this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the name of Redis nor the names of its contributors may be used
15
+ * to endorse or promote products derived from this software without
16
+ * specific prior written permission.
17
+ *
18
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ * POSSIBILITY OF SUCH DAMAGE.
29
+ */
30
+
31
+ #ifndef __ADLIST_H__
32
+ #define __ADLIST_H__
33
+
34
+ /* Node, List, and Iterator are the only data structures used currently. */
35
+
36
+ typedef struct listNode {
37
+ struct listNode *prev;
38
+ struct listNode *next;
39
+ void *value;
40
+ } listNode;
41
+
42
+ typedef struct listIter {
43
+ listNode *next;
44
+ int direction;
45
+ } listIter;
46
+
47
+ typedef struct list {
48
+ listNode *head;
49
+ listNode *tail;
50
+ void *(*dup)(void *ptr);
51
+ void (*free)(void *ptr);
52
+ int (*match)(void *ptr, void *key);
53
+ unsigned int len;
54
+ } list;
55
+
56
+ /* Functions implemented as macros */
57
+ #define listLength(l) ((l)->len)
58
+ #define listFirst(l) ((l)->head)
59
+ #define listLast(l) ((l)->tail)
60
+ #define listPrevNode(n) ((n)->prev)
61
+ #define listNextNode(n) ((n)->next)
62
+ #define listNodeValue(n) ((n)->value)
63
+
64
+ #define listSetDupMethod(l,m) ((l)->dup = (m))
65
+ #define listSetFreeMethod(l,m) ((l)->free = (m))
66
+ #define listSetMatchMethod(l,m) ((l)->match = (m))
67
+
68
+ #define listGetDupMethod(l) ((l)->dup)
69
+ #define listGetFree(l) ((l)->free)
70
+ #define listGetMatchMethod(l) ((l)->match)
71
+
72
+ /* Prototypes */
73
+ list *listCreate(void);
74
+ void listRelease(list *list);
75
+ list *listAddNodeHead(list *list, void *value);
76
+ list *listAddNodeTail(list *list, void *value);
77
+ list *listInsertNode(list *list, listNode *old_node, void *value, int after);
78
+ void listDelNode(list *list, listNode *node);
79
+ listIter *listGetIterator(list *list, int direction);
80
+ listNode *listNext(listIter *iter);
81
+ void listReleaseIterator(listIter *iter);
82
+ list *listDup(list *orig);
83
+ listNode *listSearchKey(list *list, void *key);
84
+ listNode *listIndex(list *list, int index);
85
+ void listRewind(list *list, listIter *li);
86
+ void listRewindTail(list *list, listIter *li);
87
+
88
+ /* Directions for iterators */
89
+ #define AL_START_HEAD 0
90
+ #define AL_START_TAIL 1
91
+
92
+ #endif /* __ADLIST_H__ */
@@ -0,0 +1,390 @@
1
+ /* A simple event-driven programming library. Originally I wrote this code
2
+ * for the Jim's event-loop (Jim is a Tcl interpreter) but later translated
3
+ * it in form of a library for easy reuse.
4
+ *
5
+ * Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
6
+ * All rights reserved.
7
+ *
8
+ * Redistribution and use in source and binary forms, with or without
9
+ * modification, are permitted provided that the following conditions are met:
10
+ *
11
+ * * Redistributions of source code must retain the above copyright notice,
12
+ * this list of conditions and the following disclaimer.
13
+ * * Redistributions in binary form must reproduce the above copyright
14
+ * notice, this list of conditions and the following disclaimer in the
15
+ * documentation and/or other materials provided with the distribution.
16
+ * * Neither the name of Redis nor the names of its contributors may be used
17
+ * to endorse or promote products derived from this software without
18
+ * specific prior written permission.
19
+ *
20
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30
+ * POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ #include <stdio.h>
34
+ #include <sys/time.h>
35
+ #include <sys/types.h>
36
+ #include <unistd.h>
37
+ #include <stdlib.h>
38
+
39
+ #include "ae.h"
40
+ #include "zmalloc.h"
41
+ #include "config.h"
42
+
43
+ /* Include the best multiplexing layer supported by this system.
44
+ * The following should be ordered by performances, descending. */
45
+ #ifdef HAVE_EPOLL
46
+ #include "ae_epoll.c"
47
+ #else
48
+ #ifdef HAVE_KQUEUE
49
+ #include "ae_kqueue.c"
50
+ #else
51
+ #include "ae_select.c"
52
+ #endif
53
+ #endif
54
+
55
+ aeEventLoop *aeCreateEventLoop(void) {
56
+ aeEventLoop *eventLoop;
57
+ int i;
58
+
59
+ eventLoop = zmalloc(sizeof(*eventLoop));
60
+ if (!eventLoop) return NULL;
61
+ eventLoop->timeEventHead = NULL;
62
+ eventLoop->timeEventNextId = 0;
63
+ eventLoop->stop = 0;
64
+ eventLoop->maxfd = -1;
65
+ eventLoop->beforesleep = NULL;
66
+ if (aeApiCreate(eventLoop) == -1) {
67
+ zfree(eventLoop);
68
+ return NULL;
69
+ }
70
+ /* Events with mask == AE_NONE are not set. So let's initialize the
71
+ * vector with it. */
72
+ for (i = 0; i < AE_SETSIZE; i++)
73
+ eventLoop->events[i].mask = AE_NONE;
74
+ return eventLoop;
75
+ }
76
+
77
+ void aeDeleteEventLoop(aeEventLoop *eventLoop) {
78
+ aeApiFree(eventLoop);
79
+ zfree(eventLoop);
80
+ }
81
+
82
+ void aeStop(aeEventLoop *eventLoop) {
83
+ eventLoop->stop = 1;
84
+ }
85
+
86
+ int aeCreateFileEvent(aeEventLoop *eventLoop, int fd, int mask,
87
+ aeFileProc *proc, void *clientData)
88
+ {
89
+ if (fd >= AE_SETSIZE) return AE_ERR;
90
+ aeFileEvent *fe = &eventLoop->events[fd];
91
+
92
+ if (aeApiAddEvent(eventLoop, fd, mask) == -1)
93
+ return AE_ERR;
94
+ fe->mask |= mask;
95
+ if (mask & AE_READABLE) fe->rfileProc = proc;
96
+ if (mask & AE_WRITABLE) fe->wfileProc = proc;
97
+ fe->clientData = clientData;
98
+ if (fd > eventLoop->maxfd)
99
+ eventLoop->maxfd = fd;
100
+ return AE_OK;
101
+ }
102
+
103
+ void aeDeleteFileEvent(aeEventLoop *eventLoop, int fd, int mask)
104
+ {
105
+ if (fd >= AE_SETSIZE) return;
106
+ aeFileEvent *fe = &eventLoop->events[fd];
107
+
108
+ if (fe->mask == AE_NONE) return;
109
+ fe->mask = fe->mask & (~mask);
110
+ if (fd == eventLoop->maxfd && fe->mask == AE_NONE) {
111
+ /* Update the max fd */
112
+ int j;
113
+
114
+ for (j = eventLoop->maxfd-1; j >= 0; j--)
115
+ if (eventLoop->events[j].mask != AE_NONE) break;
116
+ eventLoop->maxfd = j;
117
+ }
118
+ aeApiDelEvent(eventLoop, fd, mask);
119
+ }
120
+
121
+ static void aeGetTime(long *seconds, long *milliseconds)
122
+ {
123
+ struct timeval tv;
124
+
125
+ gettimeofday(&tv, NULL);
126
+ *seconds = tv.tv_sec;
127
+ *milliseconds = tv.tv_usec/1000;
128
+ }
129
+
130
+ static void aeAddMillisecondsToNow(long long milliseconds, long *sec, long *ms) {
131
+ long cur_sec, cur_ms, when_sec, when_ms;
132
+
133
+ aeGetTime(&cur_sec, &cur_ms);
134
+ when_sec = cur_sec + milliseconds/1000;
135
+ when_ms = cur_ms + milliseconds%1000;
136
+ if (when_ms >= 1000) {
137
+ when_sec ++;
138
+ when_ms -= 1000;
139
+ }
140
+ *sec = when_sec;
141
+ *ms = when_ms;
142
+ }
143
+
144
+ long long aeCreateTimeEvent(aeEventLoop *eventLoop, long long milliseconds,
145
+ aeTimeProc *proc, void *clientData,
146
+ aeEventFinalizerProc *finalizerProc)
147
+ {
148
+ long long id = eventLoop->timeEventNextId++;
149
+ aeTimeEvent *te;
150
+
151
+ te = zmalloc(sizeof(*te));
152
+ if (te == NULL) return AE_ERR;
153
+ te->id = id;
154
+ aeAddMillisecondsToNow(milliseconds,&te->when_sec,&te->when_ms);
155
+ te->timeProc = proc;
156
+ te->finalizerProc = finalizerProc;
157
+ te->clientData = clientData;
158
+ te->next = eventLoop->timeEventHead;
159
+ eventLoop->timeEventHead = te;
160
+ return id;
161
+ }
162
+
163
+ int aeDeleteTimeEvent(aeEventLoop *eventLoop, long long id)
164
+ {
165
+ aeTimeEvent *te, *prev = NULL;
166
+
167
+ te = eventLoop->timeEventHead;
168
+ while(te) {
169
+ if (te->id == id) {
170
+ if (prev == NULL)
171
+ eventLoop->timeEventHead = te->next;
172
+ else
173
+ prev->next = te->next;
174
+ if (te->finalizerProc)
175
+ te->finalizerProc(eventLoop, te->clientData);
176
+ zfree(te);
177
+ return AE_OK;
178
+ }
179
+ prev = te;
180
+ te = te->next;
181
+ }
182
+ return AE_ERR; /* NO event with the specified ID found */
183
+ }
184
+
185
+ /* Search the first timer to fire.
186
+ * This operation is useful to know how many time the select can be
187
+ * put in sleep without to delay any event.
188
+ * If there are no timers NULL is returned.
189
+ *
190
+ * Note that's O(N) since time events are unsorted.
191
+ * Possible optimizations (not needed by Redis so far, but...):
192
+ * 1) Insert the event in order, so that the nearest is just the head.
193
+ * Much better but still insertion or deletion of timers is O(N).
194
+ * 2) Use a skiplist to have this operation as O(1) and insertion as O(log(N)).
195
+ */
196
+ static aeTimeEvent *aeSearchNearestTimer(aeEventLoop *eventLoop)
197
+ {
198
+ aeTimeEvent *te = eventLoop->timeEventHead;
199
+ aeTimeEvent *nearest = NULL;
200
+
201
+ while(te) {
202
+ if (!nearest || te->when_sec < nearest->when_sec ||
203
+ (te->when_sec == nearest->when_sec &&
204
+ te->when_ms < nearest->when_ms))
205
+ nearest = te;
206
+ te = te->next;
207
+ }
208
+ return nearest;
209
+ }
210
+
211
+ /* Process time events */
212
+ static int processTimeEvents(aeEventLoop *eventLoop) {
213
+ int processed = 0;
214
+ aeTimeEvent *te;
215
+ long long maxId;
216
+
217
+ te = eventLoop->timeEventHead;
218
+ maxId = eventLoop->timeEventNextId-1;
219
+ while(te) {
220
+ long now_sec, now_ms;
221
+ long long id;
222
+
223
+ if (te->id > maxId) {
224
+ te = te->next;
225
+ continue;
226
+ }
227
+ aeGetTime(&now_sec, &now_ms);
228
+ if (now_sec > te->when_sec ||
229
+ (now_sec == te->when_sec && now_ms >= te->when_ms))
230
+ {
231
+ int retval;
232
+
233
+ id = te->id;
234
+ retval = te->timeProc(eventLoop, id, te->clientData);
235
+ processed++;
236
+ /* After an event is processed our time event list may
237
+ * no longer be the same, so we restart from head.
238
+ * Still we make sure to don't process events registered
239
+ * by event handlers itself in order to don't loop forever.
240
+ * To do so we saved the max ID we want to handle.
241
+ *
242
+ * FUTURE OPTIMIZATIONS:
243
+ * Note that this is NOT great algorithmically. Redis uses
244
+ * a single time event so it's not a problem but the right
245
+ * way to do this is to add the new elements on head, and
246
+ * to flag deleted elements in a special way for later
247
+ * deletion (putting references to the nodes to delete into
248
+ * another linked list). */
249
+ if (retval != AE_NOMORE) {
250
+ aeAddMillisecondsToNow(retval,&te->when_sec,&te->when_ms);
251
+ } else {
252
+ aeDeleteTimeEvent(eventLoop, id);
253
+ }
254
+ te = eventLoop->timeEventHead;
255
+ } else {
256
+ te = te->next;
257
+ }
258
+ }
259
+ return processed;
260
+ }
261
+
262
+ /* Process every pending time event, then every pending file event
263
+ * (that may be registered by time event callbacks just processed).
264
+ * Without special flags the function sleeps until some file event
265
+ * fires, or when the next time event occurrs (if any).
266
+ *
267
+ * If flags is 0, the function does nothing and returns.
268
+ * if flags has AE_ALL_EVENTS set, all the kind of events are processed.
269
+ * if flags has AE_FILE_EVENTS set, file events are processed.
270
+ * if flags has AE_TIME_EVENTS set, time events are processed.
271
+ * if flags has AE_DONT_WAIT set the function returns ASAP until all
272
+ * the events that's possible to process without to wait are processed.
273
+ *
274
+ * The function returns the number of events processed. */
275
+ int aeProcessEvents(aeEventLoop *eventLoop, int flags)
276
+ {
277
+ int processed = 0, numevents;
278
+
279
+ /* Nothing to do? return ASAP */
280
+ if (!(flags & AE_TIME_EVENTS) && !(flags & AE_FILE_EVENTS)) return 0;
281
+
282
+ /* Note that we want call select() even if there are no
283
+ * file events to process as long as we want to process time
284
+ * events, in order to sleep until the next time event is ready
285
+ * to fire. */
286
+ if (eventLoop->maxfd != -1 ||
287
+ ((flags & AE_TIME_EVENTS) && !(flags & AE_DONT_WAIT))) {
288
+ int j;
289
+ aeTimeEvent *shortest = NULL;
290
+ struct timeval tv, *tvp;
291
+
292
+ if (flags & AE_TIME_EVENTS && !(flags & AE_DONT_WAIT))
293
+ shortest = aeSearchNearestTimer(eventLoop);
294
+ if (shortest) {
295
+ long now_sec, now_ms;
296
+
297
+ /* Calculate the time missing for the nearest
298
+ * timer to fire. */
299
+ aeGetTime(&now_sec, &now_ms);
300
+ tvp = &tv;
301
+ tvp->tv_sec = shortest->when_sec - now_sec;
302
+ if (shortest->when_ms < now_ms) {
303
+ tvp->tv_usec = ((shortest->when_ms+1000) - now_ms)*1000;
304
+ tvp->tv_sec --;
305
+ } else {
306
+ tvp->tv_usec = (shortest->when_ms - now_ms)*1000;
307
+ }
308
+ if (tvp->tv_sec < 0) tvp->tv_sec = 0;
309
+ if (tvp->tv_usec < 0) tvp->tv_usec = 0;
310
+ } else {
311
+ /* If we have to check for events but need to return
312
+ * ASAP because of AE_DONT_WAIT we need to se the timeout
313
+ * to zero */
314
+ if (flags & AE_DONT_WAIT) {
315
+ tv.tv_sec = tv.tv_usec = 0;
316
+ tvp = &tv;
317
+ } else {
318
+ /* Otherwise we can block */
319
+ tvp = NULL; /* wait forever */
320
+ }
321
+ }
322
+
323
+ numevents = aeApiPoll(eventLoop, tvp);
324
+ for (j = 0; j < numevents; j++) {
325
+ aeFileEvent *fe = &eventLoop->events[eventLoop->fired[j].fd];
326
+ int mask = eventLoop->fired[j].mask;
327
+ int fd = eventLoop->fired[j].fd;
328
+ int rfired = 0;
329
+
330
+ /* note the fe->mask & mask & ... code: maybe an already processed
331
+ * event removed an element that fired and we still didn't
332
+ * processed, so we check if the event is still valid. */
333
+ if (fe->mask & mask & AE_READABLE) {
334
+ rfired = 1;
335
+ fe->rfileProc(eventLoop,fd,fe->clientData,mask);
336
+ }
337
+ if (fe->mask & mask & AE_WRITABLE) {
338
+ if (!rfired || fe->wfileProc != fe->rfileProc)
339
+ fe->wfileProc(eventLoop,fd,fe->clientData,mask);
340
+ }
341
+ processed++;
342
+ }
343
+ }
344
+ /* Check time events */
345
+ if (flags & AE_TIME_EVENTS)
346
+ processed += processTimeEvents(eventLoop);
347
+
348
+ return processed; /* return the number of processed file/time events */
349
+ }
350
+
351
+ /* Wait for millseconds until the given file descriptor becomes
352
+ * writable/readable/exception */
353
+ int aeWait(int fd, int mask, long long milliseconds) {
354
+ struct timeval tv;
355
+ fd_set rfds, wfds, efds;
356
+ int retmask = 0, retval;
357
+
358
+ tv.tv_sec = milliseconds/1000;
359
+ tv.tv_usec = (milliseconds%1000)*1000;
360
+ FD_ZERO(&rfds);
361
+ FD_ZERO(&wfds);
362
+ FD_ZERO(&efds);
363
+
364
+ if (mask & AE_READABLE) FD_SET(fd,&rfds);
365
+ if (mask & AE_WRITABLE) FD_SET(fd,&wfds);
366
+ if ((retval = select(fd+1, &rfds, &wfds, &efds, &tv)) > 0) {
367
+ if (FD_ISSET(fd,&rfds)) retmask |= AE_READABLE;
368
+ if (FD_ISSET(fd,&wfds)) retmask |= AE_WRITABLE;
369
+ return retmask;
370
+ } else {
371
+ return retval;
372
+ }
373
+ }
374
+
375
+ void aeMain(aeEventLoop *eventLoop) {
376
+ eventLoop->stop = 0;
377
+ while (!eventLoop->stop) {
378
+ if (eventLoop->beforesleep != NULL)
379
+ eventLoop->beforesleep(eventLoop);
380
+ aeProcessEvents(eventLoop, AE_ALL_EVENTS);
381
+ }
382
+ }
383
+
384
+ char *aeGetApiName(void) {
385
+ return aeApiName();
386
+ }
387
+
388
+ void aeSetBeforeSleepProc(aeEventLoop *eventLoop, aeBeforeSleepProc *beforesleep) {
389
+ eventLoop->beforesleep = beforesleep;
390
+ }